Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- param (
- [parameter( Mandatory = $true )][string]$OrganizationalUnit,
- [psobject]$Script = ( Get-Content -Path "$PSScriptRoot\update.ps1" ),
- [int]$Threads = 64
- )
- $Credential = Get-Credential
- $UserName = $Credential.UserName
- $Password = $Credential.GetNetworkCredential().Password
- function Export-ServerList
- {
- param (
- [string]$OrganizationalUnit
- )
- $ServerList = dsquery.exe computer $OrganizationalUnit -o rdn -limit 0
- for ( $i = 0; $i -lt $ServerList.Count; $i++ )
- {
- $ServerList[$i] = $ServerList[$i] -replace '"', ''
- }
- return $ServerList
- }
- function Run-Schdule
- {
- param (
- [string]$ComputerName
- )
- Invoke-Command -ComputerName $ComputerName -ScriptBlock {
- param (
- [psobject]$Script,
- [string]$UserName,
- [string]$Password
- )
- $Script | Out-File -FilePath $env:SystemRoot\System32\update.ps1 -Force
- schtasks.exe /Create /TN WindowsUpdate /TR "powershell.exe -ExecutionPolicy Bypass -File update.ps1" `
- /SC Daily /ST 02:00 /RU $UserName /RP $Password /F
- } -ArgumentList $Script, $UserName, $Password
- }
- Write-Host "Step 1: Exporting server list..."
- $ServerList = Export-ServerList -OrganizationalUnit $OrganizationalUnit
- Write-Host "Step 2: Running jobs in parallel..."
- $ServerList | Invoke-Parallel -ThrottleLimit $Threads -ScriptBlock {
- Run-Schdule -ComputerName $_
- }
- Write-Host "finished."
Add Comment
Please, Sign In to add comment