Reputation: 51
I have read all I can read but I just don't get what the problem is when exporting something from PowerShell to a CSV and getting System.Object[]
This is just a section of code I use to extract missing updates from servers that are managed by SCCM. The Switch Array is there because I need to have the SCCM Deployment Unique ID translated into a "Friendly" name I use to identify that patching collection.
This works fine and is displayed correcting on the screen (I just do this for testing the Switch array to ensure it is working by putting $Updates
in the script). However, when I attempt to export to a CSVfile, I get the System.Object[]
underneath the column titles.
I know I could pipe the first line, then select the objects in the $TargetedUpdates
array, and export them without any problem. But this only gives me the SCCM Deployment Unique ID with the server. I need to "resolve" this to a friendly name that makes sense in the CSV file. How can this be accomplished?
$TargetedUpdates = Get-WmiObject -Query "Select * from CCM_TargetedUpdateEX1 where UpdateState = 0" -Namespace root\ccm\SoftwareUpdates\DeploymentAgent -Computer ifdpv02
ForEach-Object {
$MissingUpdates = $TargetedUpdates.RefAssignments.TrimEnd(";")
$MonthlyPatch = switch ($MissingUpdates){
"{0C3267EE-F343-4577-B1A3-C24FA0406DDF}" {"October 2014 Patching for Test\DEV Servers"}
"{D849903A-4594-4D72-9224-39DC2ABA22E}" {"October 2014 Patching for Production Servers"}
"{A3F0E8A2-FB2F-4045-8E22-7726007844E6}" {"October 2014 Patching for Manual Servers"}
"{DC3991B7-30EB-4529-AA63-537968A651D0}" {"October 2014 Patching for New Server Builds"}
"{7C263094-4DA3-4AB8-9F79-0C169EA18D6D}" {"October 2014 Patching for Manual Test Servers"}
"{39EDE4AD-71C9-4393-B849-498C6D677FFF}" {"October 2014 Patching for Test\SQL Servers"}
#**********************************************************************************************
#**********************************************************************************************
default {"This is a System Center Endpoint Protection Update"}
}
$Updates = New-Object PSobject
$Updates | Add-Member NoteProperty Server ($TargetedUpdates.PScomputerName)
$Updates | Add-Member NoteProperty MonthlyPatch $MonthlyPatch
$Updates
$Updates | Export-Csv C:\temp\test.csv -NoTypeInformation
}
Invoke-Item C:\temp\test.csv
Upvotes: 0
Views: 1095
Reputation: 51
I believe I solved the problem by changing my variable for $MonthlyPatchAssignment. Excuse me for the clutter, the code I will post here is much different than I originally posted. The key seems to be grabbing the variable in the pipeline ($.RefAssignments) and putting that into my Swich array. Before I was trying to take that same variable but make it equal to another variable (i.e. $MissingUpdates = $.RefAssignments.TrimEnd(";"))
Now I am getting the correct value instead of System.Object[] in my CSV file
$MonthlyPatchAssignment = switch ($_.RefAssignments.TrimEnd(";")) {
"{0C3267EE-F343-4577-B1A3-C24FA0406DDF}" {"October 2014 Patching for Test\DEV Servers"}
"{D849903A-4594-4D72-9224-39DC2ABA22E}" {"October 2014 Patching for Production Servers"}
"{A3F0E8A2-FB2F-4045-8E22-7726007844E6}" {"October 2014 Patching for Manual Servers"}
"{DC3991B7-30EB-4529-AA63-537968A651D0}" {"October 2014 Patching for New Server Builds"}
"{7C263094-4DA3-4AB8-9F79-0C169EA18D6D}" {"October 2014 Patching for Manual Test Servers"}
"{39EDE4AD-71C9-4393-B849-498C6D677FFF}" {"October 2014 Patching for Test\SQL Servers"}
default {"This is a System Center Endpoint Protection Update"}
}
$NonCompliantDetail = New-Object PSobject
$NonCompliantDetail | Add-Member NoteProperty Server $($_.PScomputerName)
$NonCompliantDetail | Add-Member NoteProperty PatchName $MonthlyPatchAssignment
$NonCompliantDetail | Add-Member NoteProperty BullentinID $uBulletinID
$NonCompliantDetail | Add-Member NoteProperty Description $uTitle
$NonCompliantDetail | Export-Csv C:\Temp\sccm\"$FileNamePreface"_MissingUpdatesRAW.csv -NoTypeInformation -Append
Upvotes: 0
Reputation: 46730
In short: You are not processing each update individually.
Unfortunately I am not familiar with the data structure of your WMI return to know if this is going to work. You need to send data to the foreach-object
loop to process is the main thing.
$TargetedUpdates = Get-WmiObject -Query "Select * from CCM_TargetedUpdateEX1 where UpdateState = 0" -Namespace root\ccm\SoftwareUpdates\DeploymentAgent -Computer ifdpv02
$TargetedUpdates | ForEach-Object {
$MissingUpdates = $_.RefAssignments.TrimEnd(";")
$MonthlyPatch = switch ($MissingUpdates){
"{0C3267EE-F343-4577-B1A3-C24FA0406DDF}" {"October 2014 Patching for Test\DEV Servers"}
"{D849903A-4594-4D72-9224-39DC2ABA22E}" {"October 2014 Patching for Production Servers"}
#TRUNCATED AS IT IS TOO LONG**********************************************************************************************
default {"This is a System Center Endpoint Protection Update"}
}
$Updates = New-Object PSobject
$Updates | Add-Member NoteProperty Server ($TargetedUpdates.PScomputerName)
$Updates | Add-Member NoteProperty MonthlyPatch $MonthlyPatch
$Updates
} | Export-Csv C:\temp\test.csv -NoTypeInformation
We loop each update individually create an object for each. Those are then sent to the Export-CSV
for output.
Another Approach
Instead of the switch
in the loop you could use a hashtable that stores all your id and friendly names and then use a calculated property to make the object for you rather easily.
$friendlies = @{
"{0C3267EE-F343-4577-B1A3-C24FA0406DDF}" = "October 2014 Patching for Test\DEV Servers"
"{D849903A-4594-4D72-9224-39DC2ABA22E}" = "October 2014 Patching for Production Servers"
# Add more here obviously.
}
$server = "ifdpv02"
$TargetedUpdates = Get-WmiObject -Query "Select * from CCM_TargetedUpdateEX1 where UpdateState = 0" -Namespace root\ccm\SoftwareUpdates\DeploymentAgent -Computer $server
$TargetedUpdates | Select-Object @{Label="Server";Expression={$server}},
@{Label="MonthlyPatch";Expression={$friendlies[$_.RefAssignments.TrimEnd(";")]}} |
Export-Csv C:\temp\test.csv -NoTypeInformation
Caveat for both solution here:
Like I said earlier I do not know the data structure. If RefAssignments
returns as array we need to add some more logic but it can be done.
Upvotes: 0