Pages

Sunday, January 26, 2014

Creating output with objects

Reporting, ah yes. I know I covered this earlier, and in great detail, but actually I only detailed one real method of outputting useful reports. I want to cover something else, kind of like creating a here statement, except with a few special abilities. Your boss would never know if you did any work if it wasn't for the ability to run reports on things. So in our endless pursuit of trying to make scripts that output simpler, better, more attractive reports - this battle is endless.

So let's get right down to it. We've already got the points of turning objects into variables, and in general I think we are a little over the 'what does the $ mean?' phase. Basically what we are doing is using New-Object, to create a new Powershell Object (PSObject), and then we are going to add members to that object. Kind of like building your own object with properties, except you define the properties and what is contained in those fields. We will use the object we create to output to CSV.

Pancake:
$TestObj = New-Object PSObject

Alright, now we have an object, and empty one, but still, we have an object. We need to put stuff into this object. So what do we do? Well, let's use my favorite command, Get-WMIObject Win32_BIOS. I bet you didn't see that coming. So let's use our common need, loop through a file of computers, get the BIOS info, put it into a nice looking spreadsheet. What we have to do to accomplish this is add members to the object. As an example, if you type out Get-WMIObject Win32_BIOS, you are going to get a bunch of values associated with this object. In your prior experience, you can use the values of this object to put it into some form of output, or grab just the particular part that you need. We are doing the same thing with our very own object, except we determine what fields are in the object, and what are in those fields. Usually gathered from other sources. This may make it a bit easier to think about. So let's add a member to our virgin object.

$TestObj = New-Object PSOBject
$TestObj | Add-Member -MemberType NoteProperty -Name "Name" -Value "SmurfCakes"
$TestObj | Add-Member -MemberType NoteProperty -Name "Pancake Flavor" -Value "Blueberries"
$TestObj

So now we have two fields, populated with our values, which I have defined. They are static values though. Of course, we can put in variables read from a whole list of pancake flavors (or serial numbers..whatever). In this case, the name of the Field is "Name", and "Pancake Flavor", with the values SmurfCakes for a name, and Blueberries for the flavor profile. -MemberType defines exactly what you are adding, in this case we are adding a NoteProperty. There are many types of members, you can specify objects as Methods, Properties, ScriptMethods, etc. However for this case, we are creating a NoteProperty. In this instance, the NoteProperty -Name "" is the name of our column, while the Value is what goes in that column.

Let's get some information from the BIOS and make some nice output.
$GetBIOS = Get-WmiObject Win32_BIOS

#Create your object
$BIOSInfo = New-Object PSObject

#Get your values
$BIOSManufacturer = $GetBIOS.Manufacturer
$BIOSSerial = $GetBIOS.SerialNumber
$BIOSVersion = $GetBIOS.SMBIOSBIOSVersion

#Add your properties
$BIOSInfo | Add-Member -MemberType NoteProperty -Name "Manufacturer" -Value "$BIOSManufacturer" | Select -ExpandProperty Manufacturer
$BIOSInfo | Add-Member -MemberType NoteProperty -Name "Serial Number" -Value "$BIOSSerial" | Select -ExpandProperty SerialNumber
$BIOSInfo | Add-Member -MemberType NoteProperty -Name "SMB Version" -Value "$BIOSVersion" | Select -ExpandProperty SMBIOSBIOSVersion
#Export
$BIOSInfo | Export-CSV C:\Working\BIOSOutput.csv -NoTypeInformation
Nothing too crazy here, we are getting our BIOS info from $GetBIOS, and then we are selecting the properties we want and putting them into variables, and then we are adding them to our $BIOSInfo object. Finally after all of that, we do an Export-CSV to get our nice, pretty output. Note that this example was built with the purpose of leveraging this capacity, usually in your scripts you won't need to select all your variables like we had to do above, since you are probably reading them from a file, combined with some properties that you will need to select.

Of course, we could simply loop this script over a ton of machines, and add our computername to identify each one in the table.
$ComputerList = Get-Content C:\Working\Computerlist.txt

foreach ($Computer in $ComputerList)
{
$GetBIOS = Get-WmiObject Win32_BIOS

#Create your object
$BIOSInfo = New-Object PSObject

#Get your values
$BIOSManufacturer = $GetBIOS.Manufacturer
$BIOSSerial = $GetBIOS.SerialNumber
$BIOSVersion = $GetBIOS.SMBIOSBIOSVersion

#Add your properties
$BIOSInfo | Add-Member -MemberType NoteProperty -Name "Computer Name" -Value "$Computer"
$BIOSInfo | Add-Member -MemberType NoteProperty -Name "Manufacturer" -Value "$BIOSManufacturer" | Select -ExpandProperty Manufacturer
$BIOSInfo | Add-Member -MemberType NoteProperty -Name "Serial Number" -Value "$BIOSSerial" | Select -ExpandProperty SerialNumber
$BIOSInfo | Add-Member -MemberType NoteProperty -Name "SMB Version" -Value "$BIOSVersion" | Select -ExpandProperty SMBIOSBIOSVersion
#Export
$BIOSInfo | Export-CSV C:\Working\BIOSOutput.csv -NoClobber -NoTypeInformation
}
If you noticed we don't need to pipe a Select -ExpandProperty for the value $Computer, that is because it is already and isolated value and not an object container on its own.

I think that is about all for writing your own custom objects to CSV and such. A fairly easy and super useful tool in your PowershellPancake kitchen.


No comments: