- The begin scriptblock contains a hashtable of settings to use for the function. The keys represent the Cim class name and the values represent the properties to return. You can use strings, hashtables, and scriptblocks.
- Use CimSessionOption with the Dcom protocol to more reliably query Windows Server 2000-2008.
- The nested foreach loops are not that great, however the keys and properties are few so the performance is still fine. Get-CimInstance is what takes a long time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | function Get -ServerInventory { < # .SYNOPSIS Performs a hardware inventory on the specified server(s). .DESCRIPTION The values returned by the inventory process may be enhanced by adding to the settings hashtable in the begin scriptblock. The settings key is the class name. The settings values supported include string, hashtable, and scriptblock. .PARAMETER ComputerName Indicates the server(s) to perform a hardware inventory. The default value is localhost. .EXAMPLE Perform an inventory on localhost. PS C:\> Get -ServerInventory TotalPhysicalMemory : 21356912640 ProcessorName : Intel( R ) Core(TM) i7-2600 CPU @ 3.40GHz Version : 6.1.7601 SerialNumber : 00371 -OEM -8992671-00008 ComputerName : WIN7DEV01 Cores : 4 Sockets : 1 .EXAMPLE Perform an inventory on 1 -N servers with an array or using Get-Content . PS C:\> "WIN2K01" , "WIN2K02" , "WIN2008R201" | Get -ServerInventory | Format-Table -AutoSize TotalPhysicalMemory ProcessorName Version SerialNumber ComputerName Cores Sockets ------------------- ------------- ------- ------------ ------------ ----- ------- 4294148096 Intel( R ) Xeon( R ) CPU E5-2680 0 @ 2.70GHz 5.2.3790 69712-640-5906017-45214 WIN2K01 1 1 2146861056 Intel( R ) Xeon( R ) CPU E5-2670 0 @ 2.60GHz 5.2.3790 69712-641-5611134-45717 WIN2K02 1 1 4294500352 Intel( R ) Xeon( R ) CPU E5-2680 0 @ 2.70GHz 6.1.7601 55041-266-0135507-84842 WIN2008R201 1 1 .LINK Windows Server 2003 incorrectly reports the number of physical multicore processors or hyperthreading -enabled processors. Apply the below hotfix to correct the reported issue. .LINK Example on retrieving the CPU count. #> [CmdletBinding()] param( [Parameter(ValueFromPipeline= $true )] [ValidateNotNullOrEmpty()] [string[]] $ComputerName = $env :COMPUTERNAME ) begin { $settings = @{ "Win32_OperatingSystem" = @( "Version" , "SerialNumber" ) "Win32_ComputerSystem" = @({param( $result , $output ) $output [ "Capacity" ] = $result | Measure-Object -Property Capacity -Sum | Select-Object -ExpandProperty Sum}) "Win32_Processor" = @(@{n= "ProcessorName" ;e={$_.Name}}, { param( $result , $output ) $processors = @( $result ) if ( $processors [0].NumberOfCores) { $output [ "Cores" ] = $processors .Count * $processors [0].NumberOfCores } else { $output [ "Cores" ] = $processors .Count } $output [ "Sockets" ] = @( $processors | Where-Object {$_.SocketDesignation} | Select-Object -Unique ).Count }) } } process { $sessions = $ComputerName | Select-Object @{n= "ComputerName" ;e={$_}} | New -CimSession -SessionOption (New -CimSessionOption -Protocol Dcom) foreach ( $session in $sessions ) { $output = @{} foreach ( $key in $settings .Keys) { $result = Get -CimInstance -CimSession $session -ClassName $key $output [ "ComputerName" ] = $result .PSComputerName foreach ( $property in $settings [ $key ]) { if( $property -is [string]) { $output [ $property ] = $result . $property } elseif ( $property -is [scriptblock]) { Invoke -Command -ScriptBlock $property -ArgumentList $result , $output } elseif ( $property -is [hashtable]) { ( $result | Select-Object -Property $property ).PSObject.Properties | ForEach-Object { $output [$_.Name] = $_.Value } } } } [PSCustomObject] $output Remove -CimSession -CimSession $session } } } |