Server Inventory

I am trying to gather server info using powershell/WMI and I am not able to extract it to a CSV. Must be something wrong that I doing. Expert advise needed please.

 

Invoke-Command -ComputerName (Get-Content .\Servers.txt) -scriptblock 

$computerSystem = Get-WmiObject -ClassName Win32_ComputerSystem -Computername $s -Property Name, Domain -ErrorAction Stop

$ComputerName = Get-WmiObject -Class win32_Computersystem -Computername $s | Select Name

$ComputerDomain = Get-WmiObject -Class win32_Computersystem  -Computername $s | Select Domain

$Manufacturer= Get-WmiObject -Class win32_Computersystem  -Computername $s | Select Manufacturer

$Model= Get-WmiObject -Class win32_Computersystem  -Computername $s | Select model

$ModelSeries= Get-WmiObject -Class win32_Computersystemproduct  -Computername $s | Select version

$fqdn = "$($computerSystem.Name).$($computerSystem.Domain)"

$SerialNumber = Get-WmiObject -Class win32_Bios -Computername $s  | Select SerialNumber

$SerNo = Get-WmiObject win32_SystemEnclosure  -Computername $s | select serialnumber

$OperatingSystem = Get-WmiObject -class Win32_OperatingSystem  -Computername $s | Select Caption

$RAM = Get-WmiObject CIM_PhysicalMemory -ComputerName $s | Measure-Object -Property capacity -sum | % {[math]::round(($_.sum / 1GB),2)} 

$Processor= Get-WmiObject -class win32_processor  -Computername $s | Select Name

$AddressWidth= Get-WmiObject -class win32_processor  -Computername $s | Select AddressWidth

$CPU= Get-WmiObject -class win32_processor  -Computername $s | Select NumberOfCore

$Diskspace= Get-WmiObject -ClassName Win32_DiskDrive -Computername $s |Select Size

$Pingstatus= get-wmiobject -Query "select * from win32_pingstatus where Address='$s'" | select statuscode

$Ipaddress=Get-WmiObject win32_networkadapterconfiguration  -Computername $s | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1

$DefaultIpGateway=Get-WmiObject win32_networkadapterconfiguration  -Computername $s | where { $_.ipaddress -like "1*" } | select -ExpandProperty DefaultIPGateway | select -First 1

 

$infoObject = New-Object PSObject 

 

Add-Member -inputObject $infoObject -memberType NoteProperty -name "Name" -value $ComputerName.Name

Add-Member -inputObject $infoObject -memberType NoteProperty -name "Domain" -value $ComputerDomain.Domain

Add-Member -inputObject $infoObject -memberType NoteProperty -name "Manufacturer" -value $Manufacturer.Manufacturer

Add-Member -inputObject $infoObject -memberType NoteProperty -name "Model" -value $Model.model

Add-Member -inputObject $infoObject -memberType NoteProperty -name "ModelSeries" -value $ModelSeries.version

Add-Member -inputObject $infoObject -memberType NoteProperty -name "FQDN" -value $fqdn.Length

Add-Member -inputObject $infoObject -memberType NoteProperty -name "SerialNumber" -value $SerialNumber.SerialNumber

Add-Member -inputObject $infoObject -memberType NoteProperty -name "SerNo" -value $SerNo.serialnumber

Add-Member -inputObject $infoObject -memberType NoteProperty -name "OperatingSystem" -value $OperatingSystem.Caption

Add-Member -inputObject $infoObject -memberType NoteProperty -name "RAM" -value $RAM.ToSByte

Add-Member -inputObject $infoObject -memberType NoteProperty -name "Processor" -value $Processor.Name

Add-Member -inputObject $infoObject -memberType NoteProperty -name "AddressWidth" -value $AddressWidth.AddressWidth

Add-Member -inputObject $infoObject -memberType NoteProperty -name "CPU" -value $CPU.NumberOfCore

Add-Member -inputObject $infoObject -memberType NoteProperty -name "DiskSpace" -value $Diskspace.Size

Add-Member -inputObject $infoObject -memberType NoteProperty -name "PingStatus" -value $Pingstatus.statuscode

Add-Member -inputObject $infoObject -memberType NoteProperty -name "IpAddress" -value $Ipaddress.Length

Add-Member -inputObject $infoObject -memberType NoteProperty -name "DefaultIPGateway" -value $DefaultIpGateway.Length

$infoObject 

}  |Select-Object  * | export-csv -NoTypeInformation -Path .\OSinfo.csv

 

 

  • Wow.  You need to seriously rethink your code.  You have done two out of the three things possible to make your code inefficient.

    • Get-WMIOjbect is an expensive call -- read that as "SLOW".  The fact that you make multiple calls to the same class is totally inefficient.  When you get the object, e.g., Computer it brings back all the properties.  Then within the script you can extract the individual properties.  For example ...
          $compSys = Get-WmiObject -ClassName Win32_ComputerSystem -Computername $s
          $ComputerName = $compSys.Name
          $ComputerDomain = $compSys.Domain

      You can do the same thing with the other classes where you are making redundant calls.
    • You need to learn how to use splatting so you can create the $infoObject without all those needless Add-Members.