Answered by:
Trouble Exporting PS1 to CSV that matches exporting txt file

Question
-
I have this script from "https://gallery.technet.microsoft.com/scriptcenter/Export-WSUS-Approvals-per-59b89a53/view/Discussions" and when i run the script or output it to txt file (.\getapprovals.ps1 > c:\temp\approvals.txt) it returns the view i want. However, i want this view to export to CSV and i have not been successful in do so. It looks like the same question has been ask on the site but i do not see the correct response. Here is a partial example of the text file that i am trying to match in a CSV.
Title Classification
----- --------------
Update for Microsoft Office 2013 (KB3039766) 32-Bit Edition Critical Updates
Update for Microsoft Office 2013 (KB3039720) 32-Bit Edition Critical UpdatesThank you!
Thursday, August 15, 2019 11:28 AM
Answers
-
Hi,
I just took a look at the script and here is a suggestion: You need to dump the output into a PSObject and then export the object to CSV. My only problem is that I cannot test this, so what I am giving you could need a bit of adjustment to work (if it does not work directly). So, you will have to replace this:
foreach ($Approval in $Approvals) { $ComputerGroup = $WSUSServer.GetComputerTargetGroup($Approval.ComputerTargetGroupId) $Update = $WSUSServer.GetUpdate($Approval.UpdateId) $ComputerGroup.Name+"`t"+$Update.Title+"`t"+$Update.Id.UpdateId+"`t"+$Update.KnowledgeBaseArticles[0] }
with this:
$Output = foreach ($Approval in $Approvals) { $ComputerGroup = $WSUSServer.GetComputerTargetGroup($Approval.ComputerTargetGroupId) $Update = $WSUSServer.GetUpdate($Approval.UpdateId) New-Object -TypeName PSObject -Property @{ GroupName = $ComputerGroup.Name Title = $Update.Title Id = $Update.Id.UpdateId KB = $Update.KnowledgeBaseArticles[0] } } $Output | Export-Csv C:\Output.csv
and the file output.csv should contain what you need. Please post back an update. Thanks in advance!
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Edited by Stoyan ChalakovMVP Thursday, August 15, 2019 12:24 PM
- Marked as answer by bslgroup Thursday, August 15, 2019 2:56 PM
Thursday, August 15, 2019 12:22 PM
All replies
-
Hi,
I just took a look at the script and here is a suggestion: You need to dump the output into a PSObject and then export the object to CSV. My only problem is that I cannot test this, so what I am giving you could need a bit of adjustment to work (if it does not work directly). So, you will have to replace this:
foreach ($Approval in $Approvals) { $ComputerGroup = $WSUSServer.GetComputerTargetGroup($Approval.ComputerTargetGroupId) $Update = $WSUSServer.GetUpdate($Approval.UpdateId) $ComputerGroup.Name+"`t"+$Update.Title+"`t"+$Update.Id.UpdateId+"`t"+$Update.KnowledgeBaseArticles[0] }
with this:
$Output = foreach ($Approval in $Approvals) { $ComputerGroup = $WSUSServer.GetComputerTargetGroup($Approval.ComputerTargetGroupId) $Update = $WSUSServer.GetUpdate($Approval.UpdateId) New-Object -TypeName PSObject -Property @{ GroupName = $ComputerGroup.Name Title = $Update.Title Id = $Update.Id.UpdateId KB = $Update.KnowledgeBaseArticles[0] } } $Output | Export-Csv C:\Output.csv
and the file output.csv should contain what you need. Please post back an update. Thanks in advance!
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Edited by Stoyan ChalakovMVP Thursday, August 15, 2019 12:24 PM
- Marked as answer by bslgroup Thursday, August 15, 2019 2:56 PM
Thursday, August 15, 2019 12:22 PM -
function Get-WSusUpdates{ Add-Type -Path 'C:\Program Files\Update Services\Api\Microsoft.UpdateServices.Administration.dll' $adminProxy = New-Object Microsoft.UpdateServices.Administration.AdminProxy $wsus = $adminProxy.GetUpdateServer() $wsus.PreferredCulture = 'en' $UpdateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope foreach ($Approval in $wsus.GetUpdateApprovals($UpdateScope)) { $targetGroup = $wsus.GetComputerTargetGroup($Approval.ComputerTargetGroupId) $update = $wsus.GetUpdate($Approval.UpdateId) [pscustomobject]@{ Name = $targetGroup.Name Title = $update.Title UpdateGUID = $update.Id.UpdateId UpdateArticleId = $update.KnowledgeBaseArticles[0] } } } Get-WSusUpdates | Export-Csv approvals.csv
\_(ツ)_/
- Edited by jrv Thursday, August 15, 2019 1:30 PM
Thursday, August 15, 2019 1:27 PM -
You should be using the WSUS CmdLets as the DLL in this is no longer supported.
https://docs.microsoft.com/en-us/powershell/module/wsus/?view=win10-ps
\_(ツ)_/
Thursday, August 15, 2019 1:43 PM -
This failed with the error: "Method invocation failed because [Microsoft.UpdateServices.Administration.AdminProxy] does not contain a method named
'GetUpdateServer'."I was running this on localhost "the server itself".
Thursday, August 15, 2019 2:58 PM -
Thanks for the quick response!Thursday, August 15, 2019 2:59 PM
-
This failed with the error: "Method invocation failed because [Microsoft.UpdateServices.Administration.AdminProxy] does not contain a method named
'GetUpdateServer'."I was running this on localhost "the server itself".
You need to upgrade your server. Also you can change it to:
$wsus = $adminProxy.GetUpdateServerInstance('localhost'.$false)
\_(ツ)_/
Thursday, August 15, 2019 3:26 PM