Showing posts with label WSUS. Show all posts
Showing posts with label WSUS. Show all posts

Monday, April 22, 2013

Script to add machines to a WSUS group automatically


#Script to add machines to a WSUS group automatically:
#The script needs Admin credentials and the WSUS Administration Console installed on the machine where it runs
 
#Initialize Variables
 $wsusGroup = [string] "ServerGroupC"
 $wsusParentGroup = [string] "All Computers"
 $date = get-date
 $date = [string] $date.day + $date.month + $date.year + $date.hour + $date.minute
 $succeslog = [string] ".\logs\" + $date + "_success.log"
 $errorlog = [string] ".\logs\" + $date + "_errors.log"
 $WindowsUpdateServer= [string] "winupdate.uvm.edu"
 $useSecureConnection = [bool] $true
 $portNumber = [int] "443"

#Instantiate Objects:
 #Required WSUS Assembly – auto installed with WSUS Administration Tools
 [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
 if (!$wsus) {
  $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($WindowsUpdateServer,$useSecureConnection,$portNumber)
 }
 $serverList = Get-Content ".\srvlist.txt"
 $updateGroups = $Wsus.GetComputerTargetGroups()
 $updateGroup = $UpdateGroups | Where-Object{$_.Name -eq $wsusgroup} | Where-Object{$_.getparenttargetgroup().name -eq $wsusparentgroup}
 $computerScope = new-object Microsoft.UpdateServices.Administration.ComputerTargetScope
 $computerScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::All
 $computers = $wsus.GetComputerTargets($computerScope)
 $wsusServers = @()
 $WsusServersShortNames = @()

#Create arrays:
# $wsusServer = Array of WSUS Computer objects
# $wsusServerShortName = Array strings, with one server RDN per line
Write-Host "Collecting Server List from WSUS…"
$computers | foreach-object {
 $wsusServer = $_.FullDomainName
 #cut off DNS suffix and store shortname
 $wsusServerShortName = $WsusServer.split(‘.’)[0]
 $wsusServers += $WsusServer
 $wsusServersShortNames += $wsusServerShortName
} #End ForEach $computers

#loop to add servers to group
ForEach ($server in $serverList)  {
  #Check if server Netbios name is present in WSUS, if present move to group – if not log an error
  $wsusComputer = $wsusServersShortNames | Where-Object {$_ -eq $server.Trim()} #Checks for a match in WSUS for the current server in the import list.
  If ($wsusComputer) {
   $searchStr = [string] $server.Trim() + "\." #String representing a RegEx match for the relative part of the server FQDN
   $wsusComputer1 = $wsusServers | where-object {$_ -match $searchStr } #Get a WSUS computer object representing the current server in the import list.
   If ($wsusComputer1.getType().Name -match "string") { #Current $wsusComptuer1 must be a [string] object, or next step will fail.
    Write-Host "$wsusComputer1 will be added to $($updateGroup.name) group"
    $computer = $wsus.GetComputerTargetByName($wsusComputer1)
    $updateGroup.AddComputerTarget($computer)
    out-file -append -inputobject "$Server added to $($updategroup.name) group" -filepath $succeslog
   }
   Else {
    #More than one server was matched in WSUS – this will happen if your regEx is not properly formed.
    write-host "count $($wsusComputer1.count)"
    Out-File -append -inputobject "$werver has ambiguous name – check server in WSUS and add to group manually" -filepath $errorlog
   } 
  } #End If $wsusComputer
 Else {
  Write-Host "$Server not found in WSUS"
  out-file -append -inputobject "$Server not found in WSUS" -filepath $errorlog
 } 
} 
#End ForEach $server

Wednesday, April 17, 2013

Computers Are Not Appearing In the Correct Computer Groups


Client-side targeting is when you use Group Policy or registry settings to move computers into target groups. For more information about how to set up client-side targeting, see Deploying Microsoft Windows Server Update Services. There are a number of reasons why computers might not appear in groups when you are using client-side targeting. Use the following information to try to resolve this problem.

Verify that the WSUS console is set to use client-side targeting

By default, the WSUS server is set to use server-side targeting. If you are using client-side targeting, you need to set an option on the WSUS server. For more information about how to set up client-side targeting, see Deploying Microsoft Windows Server Update Services.

Verify that target computer group names match groups on the WSUS server

Make sure the name of the target computer group in Group Policy matches the name of the computer group on the WSUS server. Check the Group Policy object (GPO) or the registry setting where you enabled client-side targeting. Make sure that there are no discrepancies between the name of the computer group used in Group Policy and the name of the group used on the server. If WSUS cannot find a computer group on the server reported by a client computer, it loads the computer into the Unassigned Computers group.

Wait an hour for changes to take effect

If you make a change to group membership by using client-side targeting and the client computer has already contacted the WSUS server, it takes an hour for the server to change the computer’s group membership. This is because WSUS uses cookies to manage group membership with client-side targeting and these cookies are set to expire after one hour.
If you cannot wait an hour, use command-line options to reset the cookie and initiate detection. For information about how to use command-line options, see Deploying Microsoft Windows Server Update Services.

Script client side targeting

With the command line WuInstall (http://www.wuinstall.com), you can also script client side targeting without having to change the registry manually:

wuInstall /install (or whatever) /targetgroup "Your WSUS Target Group"

With the /targetgroup switch, you can specify the group you want to have in the TargetGroup key … WuInstall changes the registry during execution, and then back to the old values.