1

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.

3
  • You are adding the same object to the array in the loop. This results in every element in the array referencing the same object - move the creation of your object and the setting of its properties inside the loop. Also, I think you may want to move the adding of the object to the array inside your if statement. Commented Jan 22, 2015 at 17:37
  • Dugas - that's how I had it originally but I kept receiving errors about unable to add-member because it already existed. I was receiving an error about not being able to add-member because that member already existed. So I moved it outside and got that much closer.I have it outside the IF right now because I plan on adding an Else to deal with the 0 rows as well and continue to keep that placemarker there. Commented Jan 22, 2015 at 17:45
  • If you received that error, then you only moved the Add-Member calls inside the loop, not the creation of the object (New-Object). Commented Jan 22, 2015 at 17:48

1 Answer 1

1

You need to create an object for each computer, not just modify that one object over and over again. Try:

$csv = Import-CSV "C:\Computer_Name_CSV_Generator.csv"
$resultsarray = @()


Foreach ($Computer_Name in $csv)
{
    If ($Computer_Name.Computer_Name -ne 0)
    {

        $getwmiSN = Get-WmiObject -ComputerName $Computer_Name.Computer_Name win32_bios

        $wmiresults = New-Object PSCustomObject -Property @{
            "Computer_Name" = $Computer_Name.Computer_Name
            "SerialNumber" = $getwmiSN.SerialNumber
        }

        $resultsarray += $wmiresults

    }

}

$resultsarray | Export-Csv "C:\ComputerSerialNumbers.csv" -NoTypeInformation -Encoding UTF8

You could also simplify it to use the pipeline-support in PowerShell. That would allow you to shorten it to something like:

Import-CSV "C:\Computer_Name_CSV_Generator.csv" |
ForEach-Object {

    if($_.Computer_Name -ne 0) {
        $getwmiSN = Get-WmiObject -ComputerName $_.Computer_Name win32_bios

        New-Object PSCustomObject -Property @{
                "Computer_Name" = $_.Computer_Name
                "SerialNumber" = $getwmiSN.SerialNumber
        }   
    }

} | Export-Csv "C:\ComputerSerialNumbers.csv" -NoTypeInformation -Encoding UTF8
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.