![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Has anyone used Powershell to script WSUS? I've written a script which will fetch a category and classification on the WSUS server; for instance 'Windows XP' and 'Critical Updates', 'Security Updates', 'Service Packs', 'Update Rollups' respectively. Most of it went smoothly however I had to jump through some hoops that I am at a lose to see why it was necessary. WSUS will return instances of these collections [Microsoft.UpdateServices.Administration.UpdateClassificationCollection] and [Microsoft.UpdateServices.Administration.UpdateCategoryCollection] These collections have the following hierarchy [Microsoft.UpdateServices.Administration.UpdateCategoryCollection] is derived from [Microsoft.UpdateServices.Administration.WsusCollection] who's base class is[System.Collections.CollectionBase] The trouble is when either of these are returned in by a function the object type which gets returned is a System.Object[]. Powershell doesn't have any problem instanciating either of these types but it will change this into a object[] The second problem is if either of these are used within a Pipeline assigment $updates = $updates2 | where {$_.IsSuperseded -eq $false} Powershell again change the type within the expression to object[] and returns the error that it can't convert System.Object[] to [Microsoft.UpdateServices.Administration.UpdateCategoryCollection] As an aside I've read hear I believe that Powershell can't work with generics does anyone know whether this is true? To the best of my knowledge these are generics thx bob |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Has anyone used Powershell to script WSUS? Bob Landau wrote: Quote: > I've written a script which will fetch a category and classification on the > WSUS server; for instance 'Windows XP' and 'Critical Updates', 'Security > Updates', 'Service Packs', 'Update Rollups' respectively. scripted against it yet. -- Hal Rottenberg Blog: http://halr9000.com Webmaster, Psi (http://psi-im.org) Co-host, PowerScripting Podcast (http://powerscripting.net) |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Has anyone used Powershell to script WSUS? Thanks Hal, Just so you know I do have the code working correctly I've found work-arounds. But I don't believe that Powershell is handling the marshaling correctly when the return value is passed back or when your assigning the results of a pipe expression. I send you a small subset of my code tomorrow. The entire code is just too big to post. "Hal Rottenberg" wrote: Quote: > Bob Landau wrote: Quote: > > I've written a script which will fetch a category and classification on the > > WSUS server; for instance 'Windows XP' and 'Critical Updates', 'Security > > Updates', 'Service Packs', 'Update Rollups' respectively. > Post some code? I have a WSUS service I could test against, but I haven't > scripted against it yet. > > > -- > > Hal Rottenberg > Blog: http://halr9000.com > Webmaster, Psi (http://psi-im.org) > Co-host, PowerScripting Podcast (http://powerscripting.net) > |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Has anyone used Powershell to script WSUS? Hal Here is about as simple a script as I can come up with. This only shows the UpdateClassificationsCollection problem since the UpdateCategoryCollection is identical. The main reason why I need these typed collections rather than object[] is IUpdateServer.GetUpdates requires these. The script assumes your running this on your WSUS box if this is not the case you'll need to change the name from localhost to XXX ================================================ Set-Alias -name OUTPUT -value Out-Null function Filter_Classification ( [string[]] $classifications = $null) { $( ## Documented as returning a collection of IUpdateClassification [Microsoft.UpdateServices.Administration.UpdateClassificationCollection] $server_Classifications = $server.GetUpdateClassifications() ) | OUTPUT Write-Host "In function: returning $($server_Classifications.GetType())`n" $server_Classifications } ########################################## Start of script ######################################### [Reflection.Assembly]::Load( 'Microsoft.UpdateServices.Administration, Version=3.0.6000.273, Culture=Neutral, PublicKeyToken=31bf3856ad364e35' ) [Microsoft.UpdateServices.Administration.IUpdateServer] $server ` = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer('localhost', $false, 80 ) ## How I'd like to call the below method is as below. This is breaks the following error [Microsoft.UpdateServices.Administration.UpdateClassificationCollection] $classifications ` = Filter_Classification 'Critical Updates', 'Security Updates', 'Service Packs', 'Update Rollups' ## Cannot convert "System.Object[]" to "Microsoft.UpdateServices.Administration.UpdateClassificationCollection". ## The work around is to call the function which returns a UpdateClassificationCollection class as the type System.Object[] $object = Filter_Classification 'Critical Updates', 'Security Updates', 'Service Packs', 'Update Rollups' ## Next create an empty WSUS classification collection [Microsoft.UpdateServices.Administration.UpdateClassificationCollection] $update_Classifications ` = new-object Microsoft.UpdateServices.Administration.UpdateClassificationCollection ## Lastly copy each [System.Object] class into the newly instanciated UpdateClassificationCollection class $object | foreach { $update_Classifications.Add($_) | Out-Null} Write-Host "Why does gettype() not understand the underlying type? `$update_Classifications is $($($update_Classifications).gettype())`n" Write-Host "`$update_Classifications -is [Microsoft.UpdateServices.Administration.UpdateClassificationCollection]" $update_Classifications -is [Microsoft.UpdateServices.Administration.UpdateClassificationCollection] ## Same problem when a the results are returned by a pipe to work it must be a generic [system.object[]] [Microsoft.UpdateServices.Administration.UpdateClassificationCollection] $update_Classifications2 ` = new-object Microsoft.UpdateServices.Administration.UpdateClassificationCollection Write-Host "`nWriting the collection to another instance using a pipeling. This fails like in the above case`n" ## Silly example but keep it simple $update_Classifications2 = $update_Classifications | % {$i=0}{Write-Host "`$update_Classifications[$i] $($_.gettype())"; $i++; $_ } |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Has anyone used Powershell to script WSUS? Bob Landau wrote: Quote: > I've written a script which will fetch a category and classification on the > WSUS server; for instance 'Windows XP' and 'Critical Updates', 'Security > Updates', 'Service Packs', 'Update Rollups' respectively. http://www.microsoft.com/technet/scr....mspx?mfr=true Some PowerShell examples there. Marco -- Microsoft MVP - Windows PowerShell http://www.microsoft.com/mvp PowerGadgets MVP http://www.powergadgets.com/mvp Blog: http://marcoshaw.blogspot.com |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Converting a BAT script into a PowerShell Script | PowerShell | |||
| Powershell and WSUS: Copy approvals | PowerShell | |||
| Powershell, WSUS, Execute Multiple Approvals per client computer | PowerShell | |||
| Re: Powershell, WSUS, Execute Multiple Approvals per client computer | PowerShell | |||
| when run powershell script as windows service ,powershell fail | PowerShell | |||