I'm attempting to import a csv file, run a serial number search against the computer names in the csv file, and export the computer name and serial number combinations to a new csv file. Here's what I've managed to make so far that is not acting as I would expect it(still new to powershell).
$csv = Import-CSV "C:\Computer_Name_CSV_Generator.csv"
$resultsarray = @()
$wmiresults = New-Object PSCustomObject
$wmiresults | Add-Member -type NoteProperty -Name Computer_Name -Value $Computer_Name.Computer_Name
$wmiresults | Add-Member -type NoteProperty -Name SerialNumber -Value $getwmiSN
Foreach ($Computer_Name in $csv)
{
If ($Computer_Name.Computer_Name -ne 0)
{
$getwmiSN = Get-WmiObject -ComputerName $Computer_Name.Computer_Name win32_bios | Select-object SerialNumber
$wmiresults.Computer_Name = $Computer_Name.Computer_Name
$wmiresults.SerialNumber = $getwmiSN.SerialNumber
}
$resultsarray += $wmiresults
}
$resultsarray | Export-Csv "C:\ComputerSerialNumbers.csv" -NoTypeInformation -Encoding UTF8
What's happening is every time the array is updated at the end of the foreach, all the data to is modified to that of the last machine/serial number run through the foreach. When it's exported, I receive a csv entirely composed of the last computer name and serial number, but in the right amount of the import csv.
The If statement is for dealing with blank lines in the original csv that result in a 0.