Here’s a sample script that demonstrates how to use the WSUS 3.0 API to (1) list all defined computer Target Groups and (2) list of all computers subscribed to a group.
Note: This script is written to utilize the default (local) WSUS server. If you wish to run it against a remote server, find the appropriate “GetUpdateServer” call, un-comment it and specify your hostname and SSL option ($true or $false).
BONUS: I’ve always thought it would be useful to have a “Line Number” feature for lists produced via the Format-Table cmdlet. I have included here a very simple function called “GetLN” that achieves this using a $script: variable which is used as a counter. Use of this function requires that you first initialize to a value of -1 and then access via an “expression” from within the format-table call.
Sample Output:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
PS C:\>./WsusListGrpMembers.ps1 Computer Target Groups on WSUS Server: "PTWSUS01" LN Name -- ---- 1 Unassigned Computers 2 Production 3 Test 4 All Computers Select a Target Group [1-4] or hit to quit : 3 You selected Line #3, Target Group: "Test" Group: "Test" contains 3 hosts LN FullDomainName IPAddress Model OSDescription LastReportedStatusTime -- -------------- --------- ----- ------------- ---------------------- 1 test01.poshtips.com 192.168.1.120 VMware Virtual Platform Windows XP Professional 7/1/2011 6:47:52 PM 2 test02.poshtips.com 192.168.1.121 VMware Virtual Platform Windows XP Professional 7/1/2011 6:47:52 PM 3 test03.poshtips.com 192.168.1.122 VMware Virtual Platform Windows XP Professional 7/1/2011 6:47:52 PM PS C:\> |
Finally, here’s the code. Just download and run on your WSUS server.
|
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
Param ([switch]$Help,[switch]$History) $HelpText = @' ####################################################################################### Name : WsusListGrpMembers.ps1 Date : 07/01/2011 Author : xb90@poshtips.com Purpose : List WSUS Target Group Members Usage : ./WsusListGrpMembers [-Help][-History] where -Help produces help output -History produces help output with maintenance history Notes : This is a read-only script; no changes will be made to the WSUS environment ####################################################################################### '@ $HistoryText = @' Maintenance Log Date By Updates (insert newest updates at top) ---------- ---- --------------------------------------------------------------------- 07/01/2011 XB90 New Script ####################################################################################### '@ if ($help -or $History){ $HelpText; if ($History){$HistoryText} exit } function GetLN{ $script:ln += 1; $script:ln } [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null if (!$wsus) { #if accessing a remote WSUS server, use the following line and update as needed (params: [string]servername, [boolean]UseSSL) #$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer("servername",$false); #if accessing a local WSUS server, use the following line $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer(); if (!$wsus) { throw "WSUS Server Connection failed" } } clear-host "Computer Target Groups on WSUS Server: `"{0}`"" -f $wsus.name | out-default $grp = $wsus.GetComputerTargetGroups() $script:ln=-1 $grp |ft @{label="LN"; expression={$(GetLn)}},Name -auto $selection = read-host ("Select a Target Group [1-{0}] or hit to quit " -f $grp.count) if (!$selection) {exit} "`nYou selected Line #{0}, Target Group: `"{1}`"" -f $selection, $grp[($selection-1)].name $comp = $grp[($selection-1)].GetComputerTargets() "`nGroup: `"{0}`" contains {1} hosts`n" -f $grp[($selection-1)].name,$comp.count $script:ln=-1 $comp |ft @{label="LN"; expression={$(GetLN)}},fulldomainname,ipaddress,model,osdescription,lastreportedstatustime -auto |
Hello , How can we modify this to script get the computers list from unassigned group and send it via email?
Hey Nishanth,
There may be a better way to do this, but off the top of my head….
Assuming the “Unassigned Computers” group is always at index zero (I think it is), once could do something like:
remove lines 54 thru 65 from the script
append the following:
$comp = $grp[0].getcomputertargets()
$comp | select FullDomainName | out-file unassignedHosts.txt
You can choose any level of detail from the $comp object, but I’ve just selected FullDomainName here.
That gets you part way there.
Next you just need to do the email part. See powershell help for the Send-Email cmdlet OR see my article ( http://poshtips.com/2009/11/09/sending-email-from-a-powershell-script/ ) if you want to send email directly via Dot Net.