To use PSObject Add-Member or not…

I was playing around creating a PowerShell script with the intention of add items to a custom PSObject using the Add-Member.  Well, I couldn’t get it to work or maybe I was still doing something wrong.  But then, I came up with a (kind-of) unorthodox method that I will share with you.  Please, don’t tell anyone:

First, here’s a PowerShell script function “Get-SPInventory” I found in Technet; (http://technet.microsoft.com/en-us/magazine/2008.12.windowspowershell.aspx?pr=blog).  There’s nothing wrong with it and work as expected:

Function Get-SPInventory {
  PROCESS {
    $wmi = Get-WmiObject Win32_OperatingSystem –comp $_ | Select CSName,BuildNumber,ServicePackMajorVersion
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty BuildNumber ($wmi.BuildNumber)
    $obj | Add-Member NoteProperty CSName ($wmi.CSName)
    $obj | Add-Member NoteProperty SPVersion ($wmi.ServicePackMajorVersion)
    $wmi = Get-WmiObject Win32_BIOS –comp $_ | Select SerialNumber
    $obj | Add-Member NoteProperty BIOSSerial ($wmi.SerialNumber)
    Write-Output $obj
  }
}
 image

So, I going to duplicate the same functionality in a different way without creating a new PSObject and not using Add-Member.  I’m going to let PowerShell to create the PSObject for me and work a little with the Select-Object.  Here’s my version:

Function Get-SPInventory2 {
    Process{
        $wmi1 = Get-WmiObject Win32_OperatingSystem –comp $_ |
Select CSName,BuildNumber,ServicePackMajorVersion
        $wmi2 = Get-WmiObject Win32_BIOS –comp $_ | select SerialNumber
        $results = $wmi1|select csname,BuildNumber,@{Label=”SPVersion”;Expression={$_.ServicePackMajorVersion -as [int]};},@{Label=”SerialNum”;Expression={$wmi2.SerialNumber};}
        Write-Output $results
    }
}

image

Now, I got both functions generating the same results.  I’m my script, the “Label=” will create the noteproperty and the “Expression=” will store the value for the $wmi2.SerialNumber. 

Let say I want to use SQL Server Management Object (SMO) to collect various information from some SQL Servers on the network.  Here’s another sample script: (make sure your SMO assembly is loaded)

$SQLSvr = [Microsoft.SqlServer.Management.Smo.SmoApplication]::EnumAvailableSqlServers($false) | Select name [Array] $SQLinfo = ForEach($SQL in $SQLSvr){
    $MySQL = new-object(‘Microsoft.SqlServer.Management.Smo.Server’) $SQL.Name
    $MySQL | Select NetName, Product, EngineEdition, Version, Platform, ProductLevel
}
$SQLinfo | select Netname, Product, EngineEdition, Version, ProductLevel | ft -autoimage

In this script, I’m using the SMO SQL enumerator to collect the SQL Server names so we can use the “foreach” to collect the information from each server into the output array “$SQLinfo”.   After building the output variable then you can save the results in different output format such as: CSV, TXT, XML, or event back to a SQL table.

This is another way to collect information and let PowerShell do the rest for you!  PowerShell gives Power to the scripters.

Check out this other blog about creating objects: http://powershell.com/cs/blogs/tobias/archive/2010/09/22/creating-objects-yourself-and-a-bunch-of-cool-things-you-can-do-with-them.aspx

Happy PowerShelling!!