我已经阅读了所有我能阅读的内容,但我只是不知道将PowerShell中的内容导出到CSV并获取System.Object []
时出现的问题这只是我用来从SCCM管理的服务器中提取缺失更新的一段代码。 Switch Array就在那里,因为我需要将SCCM部署唯一ID翻译成我用来识别该修补集合的“友好”名称。
这样可以正常显示并在屏幕上显示(我只是这样做以测试Switch数组,以确保它在脚本中放置$Updates
)。但是,当我尝试导出到CSV文件时,我会在列标题下面找到System.Object[]
。
我知道我可以管道第一行,然后选择$TargetedUpdates
数组中的对象,并导出它们没有任何问题。但这只能为我提供服务器的SCCM部署唯一ID。我需要将其“解析”为一个在CSV文件中有意义的友好名称。如何实现这一目标?
$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
答案 0 :(得分:0)
我相信我通过更改$ MonthlyPatchAssignment的变量来解决问题。请原谅我的杂乱,我将在这里发布的代码与我最初发布的代码有很大不同。关键似乎是抓住管道中的变量($ .RefAssignments)并将其放入我的Swich数组中。在我尝试使用相同的变量但使其等于另一个变量之前(即$ MissingUpdates = $ .RefAssignments.TrimEnd(";"))
现在我在CSV文件中获取了正确的值而不是System.Object []
$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