Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Has anyone used Powershell to script WSUS?

Reply
 
Old 11-25-2007   #1 (permalink)
Bob Landau


 
 

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 SpecsSystem Spec
Old 11-26-2007   #2 (permalink)
Hal Rottenberg


 
 

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.
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 SpecsSystem Spec
Old 11-26-2007   #3 (permalink)
Bob Landau


 
 

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 SpecsSystem Spec
Old 11-27-2007   #4 (permalink)
Bob Landau


 
 

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 SpecsSystem Spec
Old 12-10-2007   #5 (permalink)
Marco Shaw [MVP]


 
 

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.
FYI:
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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46