Home > Software engineering >  Get serialnumber from asset list
Get serialnumber from asset list

Time:08-02

Started in recent weeks in a Junior infrastructure role, and begun playing around with powershell to help save some time here and there.

I am trying to do the following: 1- I'm port a CSV file with a single column names asset 2- Perform a "ForEach" check on each line to find the device's serial number 3- Output results to a CSV with two column "asset" and "serialnumber"

I have dabbled in a few areas, and am currently sitting at something like this:

$file1 = Import-Csv -path "c:\temp\assets.csv" | ForEach-Object {
$asset = $_.asset
}
wmic /node:$asset bios get serialnumber
Export-Csv -Path "c:\temp\assetandserial.csv" -NoTypeInformation

As you may or may not see, I tried to set the column labelled "asset" as the variable, however, not sure if I placed it correctly. I have tried a few other things, but honestly it's all new to me, so I haven't the foggiest idea where to go from here.

CodePudding user response:

  • wmic is deprecated, and, for rich type support (OO processing), using PowerShell-native cmdlets is preferable in general.

  • wmic's immediate PowerShell counterpart is Get-WmiObject, which, however, is equally deprecated, in favor of Get-CimInstance.

    • Important: The command below uses Get-CimInstance, but note that the CIM cmdlets use a different remoting protocol than the obsolete WMI cmdlets. In short: To use the CIM cmdlets with remote computers, those computers must be set up in the same way that PowerShell remoting requires - see this answer for details.
Get-CimInstance Win32_BIOS -ComputerName (Import-Csv c:\temp\assets.csv).asset | 
  Select-Object @{ n='Asset'; e='PSComputerName' }, SerialNumber |
  Sort-Object Asset |
  Export-Csv c:\temp\assetandserial.csv -NoTypeInformation
  • Note the use of member-access enumeration to extract all .asset values directly from the collection of objects returned from Import-Csv.

  • All computer (asset) names are passed at once to Get-CimInstance, which queries them in parallel. Since the ordering of the responses from the targeted remote machines isn't guaranteed, Sort-Object is used to sort the results.

  • A calculated property is used with Select-Object to rename the automatically added .PSComputerName property to Asset.

  • Related