12 Objects: The best kind of output

 

Remember back in chapter 8 when we created our initial design for Get-MachineInfo? So far, we’re querying for some information but not for everything we want it to do. That was a deliberate decision we made so that you could get some structure around the tool first. We’ve also held off because once you start querying a bunch of information, you need to take a specific approach to combine it, and we wanted to tackle that approach in a single chapter.

Right now, the “functional” part of the tool looks like this:

# Query data
$Session = New-CIMSession -ComputerName SRV1
$os = Get-CimInstance -ClassName Win32_OperatingSystem `
                      -CimSession $session
# Close session
$session | Remove-CimSession
# Output data
$os | Select-Object -Prop @{n='ComputerName';e={$computer}},
                        Version,ServicePackMajorVersion

We’re using Select-Object to produce the pieces of output we want. Some might call this the lazy way, but we’re just reducing the information we gathered, which someone could have done entirely alone. Let’s go back to the list of the information we originally wanted and add where we’ll get the information from:

12.1 Assembling the information

12.2 Constructing and emitting output

12.3 A quick test

12.4 An object alternative

12.5 Enriching objects

12.6 Your turn

12.6.1 Start here

12.6.2 Your task

12.6.3 Our take

Summary