|
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++; $_ } |