Guest User

Untitled

a guest
Jan 26th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. param (
  2. [parameter( Mandatory = $true )][string]$OrganizationalUnit,
  3. [psobject]$Script = ( Get-Content -Path "$PSScriptRoot\update.ps1" ),
  4. [int]$Threads = 64
  5. )
  6.  
  7. $Credential = Get-Credential
  8. $UserName = $Credential.UserName
  9. $Password = $Credential.GetNetworkCredential().Password
  10.  
  11. function Export-ServerList
  12. {
  13. param (
  14. [string]$OrganizationalUnit
  15. )
  16.  
  17. $ServerList = dsquery.exe computer $OrganizationalUnit -o rdn -limit 0
  18. for ( $i = 0; $i -lt $ServerList.Count; $i++ )
  19. {
  20. $ServerList[$i] = $ServerList[$i] -replace '"', ''
  21. }
  22.  
  23. return $ServerList
  24. }
  25.  
  26. function Run-Schdule
  27. {
  28. param (
  29. [string]$ComputerName
  30. )
  31.  
  32. Invoke-Command -ComputerName $ComputerName -ScriptBlock {
  33. param (
  34. [psobject]$Script,
  35. [string]$UserName,
  36. [string]$Password
  37. )
  38.  
  39. $Script | Out-File -FilePath $env:SystemRoot\System32\update.ps1 -Force
  40.  
  41. schtasks.exe /Create /TN WindowsUpdate /TR "powershell.exe -ExecutionPolicy Bypass -File update.ps1" `
  42. /SC Daily /ST 02:00 /RU $UserName /RP $Password /F
  43. } -ArgumentList $Script, $UserName, $Password
  44. }
  45.  
  46. Write-Host "Step 1: Exporting server list..."
  47. $ServerList = Export-ServerList -OrganizationalUnit $OrganizationalUnit
  48.  
  49. Write-Host "Step 2: Running jobs in parallel..."
  50. $ServerList | Invoke-Parallel -ThrottleLimit $Threads -ScriptBlock {
  51. Run-Schdule -ComputerName $_
  52. }
  53.  
  54. Write-Host "finished."
Add Comment
Please, Sign In to add comment