Advertisement
Guest User

Script (cmdlet) en PowerShell para mover equipos en el WSUS

a guest
Sep 12th, 2012
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Script o cmdlet en PowerShell para agregar o mover equipos a un grupo del WSUS (sin SSL), a partir de un listado de equipos, en un archivo de texto.
  2. #El script necesita Permisos de administrador y tener instalada la consola del WSUS en la maquina local donde se ejecuta el script.  
  3. Más info > http://jogacrack.co/2012/08/17/administrar-wsus-powershell-scripts-windows
  4. ------------------------------------
  5. #Inicializar variables
  6.     $wsusGroup = [string] "GrupoparaMoverLosEquipos"
  7.     $date = get-date
  8.     $date = [string] $date.day + $date.month + $date.year + $date.hour + $date.minute
  9.     $succeslog = [string] ".\logs\" + $date + "_success.log"
  10.     $errorlog = [string] ".\logs\" + $date + "_errors.log"
  11.     $WindowsUpdateServer= [string] "NombreServerWSUS"
  12.  
  13.  
  14. #Inicializar Objetos:
  15.     #Requiere la consola del WSUS instalada con las herramientas administrativas.
  16.     [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
  17.     if (!$wsus) {
  18.         $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($WindowsUpdateServer,$False)
  19.     }
  20.     $serverList = Get-Content ".\listadoequipos.txt"
  21.     $updateGroups = $Wsus.GetComputerTargetGroups()
  22.     $updateGroup = $UpdateGroups | Where-Object{$_.Name -eq $wsusGroup}
  23.     $computerScope = new-object Microsoft.UpdateServices.Administration.ComputerTargetScope
  24.     $computerScope.IncludedInstallationStates = [Microsoft.UpdateServices.Administration.UpdateInstallationStates]::All
  25.     $computers = $wsus.GetComputerTargets($computerScope)
  26.     $wsusServers = @()
  27.     $WsusServersShortNames = @()
  28.  
  29. #Crear arrays (estructura de datos):
  30. # $wsusServer = Array de los objetos (equipos) del WSUS.
  31. # $wsusServerShortName = Cadena de Arrays, con un servidor RDN por línea
  32. Write-Host "Recolectando lista de los equipos desde el WSUS"
  33. $computers | foreach-object {
  34.     $wsusServer = $_.FullDomainName
  35.     $wsusServerShortName = $WsusServer.split(‘.’)[0]
  36.     $wsusServers += $WsusServer
  37.     $wsusServersShortNames += $wsusServerShortName
  38. } #Fin del ciclo For de los equipos
  39.  
  40. #Ciclo FOR para agregar o mover los equipos al grupo en el WSUS
  41. ForEach ($server in $serverList)  {
  42.         #Chequear si el nombre Netbios de los equipos esta en el WSUS, si esta se mueve sino genera el error y lo escribe en el LOG.
  43.         $wsusComputer = $wsusServersShortNames | Where-Object {$_ -eq $server.Trim()}
  44.         If ($wsusComputer) {
  45.             $searchStr = [string] $server.Trim() + "\."
  46.             $wsusComputer1 = $wsusServers | where-object {$_ -match $searchStr }
  47.             If ($wsusComputer1.getType().Name -match "string") {
  48.                 Write-Host "$wsusComputer1 se agregara al grupo $($updateGroup.name) "
  49.                 $computer = $wsus.GetComputerTargetByName($wsusComputer1)
  50.                 $updateGroup.AddComputerTarget($computer)
  51.                 out-file -append -inputobject "$Server agregado al grupo $($updategroup.name) " -filepath $succeslog
  52.             }
  53.             Else {
  54.                 write-host "count $($wsusComputer1.count)"
  55.                 Out-File -append -inputobject "$Server el nombre es ambiguo, por favor verifique en el WSUS y agregue el equipo manualmente" -filepath $errorlog
  56.             }
  57.         } #Fin del si $wsusComputer
  58.     Else {
  59.         Write-Host "$Server no encontrado en el WSUS"
  60.         out-file -append -inputobject "$Server no encontrado en el WSUS" -filepath $errorlog
  61.     }
  62. } #End ForEach $server
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement