This is a follow-up to an earlier post (How to output a bunch of variables into a table.) Now that I can ouput data for a single computer I need to to do the same for a list of computers into a CSV. In more general terms how do I get a bunch of $Computer objects into a single collection/array/object/thingy with a clever name like $Computers?
# http://ss64.com/ps/get-wmiobject-win32.html # http://social.technet.microsoft.com/Forums/en-US/da54b6ab-6941-4e45-8697-1d3236ba2154/powershell-number-of-cpu-sockets-wmi-query?forum=winserverpowershell # http://serverfault.com/questions/10328/determine-cpu-processors-vs-sockets-though-wmi # http://social.technet.microsoft.com/Forums/windowsserver/en-US/8443fcfd-5a0b-4c3d-bda7-26df83d2ee92/how-to-output-a-bunch-of-variables-into-a-table?forum=winserverpowershell Param( [Parameter(Mandatory=$false,ValueFromPipeline=$true)] [string]$ComputerName = $env:COMPUTERNAME, [Parameter(Mandatory=$false,ValueFromPipeline=$false)] [ValidateScript( { If ( $_ -ne $null ) { Test-Path $_ } } )] [String]$ComputerListFile ) Function Get-Computer { Param( [string]$ComputerName ) $Win32_PingStatus = $null $Win32_PingStatus_Result = $null $Win32_OperatingSystem = $null $Win32_Processor = $null $Win32_PhysicalMemory = $null $Win32_ComputerSystem = $null $Win32_BIOS = $null $Computer = $null $Win32_PingStatus = "select * from Win32_PingStatus where address = '$ComputerName'" $Win32_PingStatus_Result = Get-WmiObject -query $Win32_PingStatus If ( $Win32_PingStatus_Result.protocoladdress ) { "$ComputerName ping succeeded." $Win32_OperatingSystem = Get-WmiObject Win32_OperatingSystem -computer $ComputerName -ErrorAction SilentlyContinue If ( $Win32_OperatingSystem -eq $null) {"$ComputerName WMI failed." } Else {"$ComputerName WMI succeeded." $Win32_Processor = [object[]]$(Get-WmiObject Win32_Processor -computer $ComputerName) $Win32_PhysicalMemory = [object[]]$(Get-WmiObject Win32_PhysicalMemory -computer $ComputerName) $Win32_ComputerSystem = Get-WmiObject Win32_ComputerSystem -computer $ComputerName $Win32_BIOS = Get-WmiObject Win32_BIOS -computer $ComputerName $Computer = New-Object -Type PSObject -Property @{ Name = $Win32_OperatingSystem.CSName Win32_BIOS_SerialNumber = [string]$Win32_BIOS.SerialNumber Win32_ComputerSystem_Manufacturer = [string]$Win32_ComputerSystem.Manufacturer Win32_ComputerSystem_Model = [string]$Win32_ComputerSystem.Model #Win32_ComputerSystem_NumberOfLogicalProcessors = [int32]$Win32_ComputerSystem.NumberOfLogicalProcessors #Win32_ComputerSystem_NumberOfProcessors = [int32]$Win32_ComputerSystem.NumberOfProcessors Win32_ComputerSystem_TotalPhysicalMemory = [long]$Win32_ComputerSystem.TotalPhysicalMemory Win32_ComputerSystem_TotalPhysicalMemory_GB = [float]($Win32_ComputerSystem.TotalPhysicalMemory / (1024*1024*1024)) Win32_OperatingSystem_Caption = [string]$Win32_OperatingSystem.Caption Win32_OperatingSystem_CSName = [string]$Win32_OperatingSystem.CSName #Win32_OperatingSystem_OSArchitecture = [string]$Win32_OperatingSystem.OSArchitecture #Win32_OperatingSystem_SerialNumber = [string]$Win32_OperatingSystem.SerialNumber Win32_OperatingSystem_ServicePackVersion = [string]$Win32_OperatingSystem.ServicePackMajorVersion + "." + [string]$Win32_OperatingSystem.ServicePackMinorVersion Win32_PhysicalMemory_Capacity = ($Win32_PhysicalMemory | ForEach-Object { $_.Capacity } | Measure-Object -Sum).sum Win32_PhysicalMemory_Capacity_GB = (($Win32_PhysicalMemory | ForEach-Object { $_.Capacity } | Measure-Object -Sum).sum / (1024*1024*1024)) Win32_Processor_Count = [int]$Win32_Processor.Count Win32_Processor_NumberOfCores = [string]$Win32_Processor[0].NumberOfCores Win32_Processor_NumberOfLogicalProcessors = [string]$Win32_Processor[0].NumberOfLogicalProcessors #Win32_Processor_Description = [string]$Win32_Processor[0].Description Win32_Processor_Manufacturer = [string]$Win32_Processor[0].Manufacturer Win32_Processor_Name = [string]$Win32_Processor[0].Name } ## end new-object $Computer } } Else {"$ComputerName ping failed." } $ComputerNameMgmt = $ComputerName + "-mgmt" $Win32_PingStatus = "select * from Win32_PingStatus where address = '$ComputerNameMgmt'" $Win32_PingStatus_Result = Get-WmiObject -query $Win32_PingStatus If ( $Win32_PingStatus_Result.protocoladdress ) { "$ComputerNameMgmt ping succeded." } Else {"$ComputerNameMgmt ping failed." } } """$(Get-Date -Format o) Starting script $($MyInvocation.MyCommand.Name)" If ( $ComputerListFile -eq $null -or $ComputerListFile.Length -eq 0 ) { "Processing computer $ComputerName""" Get-Computer( $ComputerName ) } Else {"Processing computer list $ComputerList""" $ComputerList = Get-Content $ComputerListFile $Computers = @{} $Results = @()"-----" ForEach( $ComputerListMember in $ComputerList ) {"$(Get-Date -Format o) $ComputerListMember" # Get-Computer( $ComputerListMember ) | Add-Member -InputObject $Computers -MemberType NoteProperty -Name $_ # http://social.technet.microsoft.com/Forums/windowsserver/en-US/e7d602a9-a808-4bbc-b6d6-dc78079aafc9/powershell-to-ping-computers # $Compuers += New-Object PSObject -Property $Props # $Computers += New-Object PSObject -Property Get-Computer( $ComputerListMember ) Get-Computer( $ComputerListMember )"-----" } } """$(Get-Date -Format o) Ending script $($MyInvocation.MyCommand.Name)"
If I try something like this:
Get-Computer( $ComputerListMember ) | Add-Member -InputObject $Computers -MemberType NoteProperty -Name $_
I get the following, even though $_.Name is not null.
Add-Member : Cannot bind argument to parameter 'Name' because it is null. At <path to my script>Get-Hardware_Memory_OSVersion_CPU_Cores_ver04_sanitized.ps1:111 char:107+ Get-Computer( $ComputerListMember ) | Add-Member -InputObject $Computers -MemberType NoteProperty -Name <<<< $_+ CategoryInfo : InvalidData: (:) [Add-Member], ParameterBindingValidationException+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.AddMemberCommand
Or if I try this:
$Computers += New-Object PSObject -Property Get-Computer( $ComputerListMember )
I get this:
New-Object : Cannot bind parameter 'Property'. Cannot convert the "Get-Computer" value of type "System.String" to type "System.Collections.Hashtable". At <path to my script>Get-Hardware_Memory_OSVersion_CPU_Cores_ver04_sanitized.ps1:114 char:47+ $Computers += New-Object PSObject -Property <<<< Get-Computer( $ComputerListMember )+ CategoryInfo : InvalidArgument: (:) [New-Object], ParameterBindingException+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.NewObjectCommand