I'm trying to get the cpu name for all of the machines on my domain. I'm running the following
Get-CimInstance -ComputerName $i.DNSHostName -Class CIM_Processor | Select-Object "PSComputerName", "Name", "NumberOfCores"
I'm taking the results of this to build a custom object
$workstationWithIp = [PSCustomObject]@{
ComputerName = $i.DNSHostName
CPU = $workstationObj.Name
Cores = $workstationObj.NumberOfCores
IP = $ip
Memory = $memory
Uptime = $uptimeHours
OS = $os.Caption " " $os.Version
}
My issue is that some of the machines have more than one processor so the "Name" will return multiple results. All of these servers have multiple of the same model so how can I get the result knocked down so I can put it into my custom object as a string? I'm using those custom objects to make a CSV and some will have the cpu name and the core fine but the ones with multiple cpus will give me System.Object[] for the name and core count in the csv
CodePudding user response:
As mentioned in the comments, a safe way to pick at most 1 item from a potential array/collection of values is by piping to Select-Object -First 1
:
[PSCustomObject]@{
ComputerName = $i.DNSHostName
CPU = $workstationObj.Name |Select-Object -First 1
Cores = $workstationObj.NumberOfCores |Select-Object -First 1
IP = $ip
Memory = $memory
Uptime = $uptimeHours
OS = $os.Caption " " $os.Version
}