I have, on a number of occasions had issues exporting data when it is sourced from multiple cmdlets.
Fore example: I am trying to export a list of printers and other relevant information using WMI but there are two cmdlets necessary to extract all the information:
Get-WmiObject -Class Win32_Printer `
-ComputerName $Computer -Credential $cred
and...
Get-WmiObject -Class Win32_PrinterConfiguration `
-ComputerName $Computer -Credential $cred
I have created an object to pipe the data into so it can be exported:
$CSVData = New-Object PSObject
$CSVData | Add-Member -MemberType NoteProperty -Name "Name" -Value ""
$CSVData | Add-Member -MemberType NoteProperty -Name "DriverName" -Value ""
$CSVData | Add-Member -MemberType NoteProperty -Name "Location" -Value ""
$CSVData | Add-Member -MemberType NoteProperty -Name "PrintProcessor" -Value ""
$CSVData | Add-Member -MemberType NoteProperty -Name "PortName" -Value ""
$CSVData | Add-Member -MemberType NoteProperty -Name "Duplex" -Value ""
$CSVData | Add-Member -MemberType NoteProperty -Name "Color" -Value ""
$CSVData | Add-Member -MemberType NoteProperty -Name "PrintQuality" -Value ""
$CSVData.Name = @()
$CSVData.DriverName = @()
$CSVData.Location = @()
$CSVData.PrintProcessor = @()
$CSVData.PortName = @()
$CSVData.Duplex = @()
$CSVData.Color = @()
$CSVData.PrintQuality = @()
I have tried to lay it out like this:
$PrinterConfig = Get-WmiObject -Class Win32_PrinterConfiguration `
-ComputerName $Computer -Credential $cred |
Select-Object Duplex,
Color,
PrintQuality
$CSVData.Duplex += $PrinterConfig.Duplex
$CSVData.Color += $PrinterConfig.Color
$CSVData.PrintQuality += $PrinterConfig.PrintQuality
And, like this:
$PrinterInfo = Get-WmiObject -Class Win32_Printer `
-ComputerName $Computer -Credential $cred |
Select-Object Name,
DriverName,
Location,
PrintProcessor,
PortName |
ForEach-Object {
$CSVData.Name += $_.Name
$CSVData.DriverName += $_.DriverName
$CSVData.Location += $_.Location
$CSVData.PrintProcessor += $_.PrintProcessor
$CSVData.PortName += $_.PortName
}
I then export like this:
$CSVData | Export-Csv -Path c:\temp\printers.csv $OutDir
The issue is that no useful data is actually exported and I'm not sure how to properly combine the output from these two cmdlets.
There is most likely a more elegant way to do this...
Please help!