Exporting to CSV: Need help combining outputs to CSV

34 Views Asked by At

I am unable to figure out how to get both the $ModelInfo and $SeriaNumber to output to the CSV on a single line.

$LocalComputer = Read-Host "Enter Local Computer Name"
$Computer = Import-Csv \\$LocalComputer\c$\Scripts\Computerlist.Csv


foreach ($Computer in $Computer) {

    $PingTest = Test-Connection $Computer.Name -Count 1 -Quiet

    if ($PingTest -eq $false) {

        Write-Host $Computer.name -ForegroundColor Red
        $Computer | Select-Object name | Export-Csv \\$LocalComputer\C$\Scripts\FailedComputer.Csv -Append

    }

    else {

        Write-Host $Computer.name -ForegroundColor Green
        Get-Service -ComputerName $Computer.name -name winrm | restart-Service
        start-sleep -s 1
        $ModelInfo = Invoke-Command -Cn $Computer.name -ScriptBlock { get-wmiobject win32_computersystem | select-object Caption, Model }
        $ModelInfo | export-csv \\$LocalComputer\C$\Scripts\Computers.Csv -Append
        $SerialNumber = Invoke-Command -Cn $Computer.name -ScriptBlock { get-wmiobject win32_bios | select-object SerialNumber }
        $SerialNumber | export-csv \\$LocalComputer\C$\Scripts\Computers.Csv -Append

    }

}

The $ModelInfo output is being overwritten by the $SerialNumber info in the CSV

1

There are 1 best solutions below

0
J P On

The issue I'm seeing is that the $ModelInfo has different column names than $SerialNumber which throws a cannot append error. You can use the -Force switch to append the $SerialNumber data into the existing columns and leave the unmatched columns blank.

Change this:

$ModelInfo = Invoke-Command -Cn $Computer.name -ScriptBlock { get-wmiobject win32_computersystem | select-object Caption, Model }
$ModelInfo | export-csv \\$LocalComputer\C$\Scripts\Computers.Csv -Append
$SerialNumber = Invoke-Command -Cn $Computer.name -ScriptBlock { get-wmiobject win32_bios | select-object SerialNumber }
$SerialNumber | export-csv \\$LocalComputer\C$\Scripts\Computers.Csv -Append

To this:

$ModelInfo = Invoke-Command -Cn $Computer.name -ScriptBlock { get-wmiobject win32_computersystem | select-object Caption, Model }
$ModelInfo | export-csv \\$LocalComputer\C$\Scripts\Computers.Csv -Append
$SerialNumber = Invoke-Command -Cn $Computer.name -ScriptBlock { get-wmiobject win32_bios | select-object SerialNumber }
$SerialNumber | export-csv \\$LocalComputer\C$\Scripts\Computers.Csv -Append -Force

This will create the Computers.csv that looks like this (I have redacted my actual info). The first line is the column names of the $ModelInfo object and the second line is the data in $ModelInfo columns and the third line is the data in $SerialNumber export without column names and forced into the columns of $ModelInfo. Note the two commas at the beginning of the third line, that is the output of the $SerialNumber object which does not contain a "Caption" or "Model" column.

"Caption","Model","PSComputerName","RunspaceId","PSShowComputerName" "ComputerName","Computer Type","ComputerName.domain.com","0000","True" ,,"ComputerName.domain.com","0000","True"