Wednesday, July 9, 2014

PowerShell - Uninstall Application for Lab Computers Simultaneously

# Loop through the server list
Get-Content -Path "C:\Scripts\Computers.txt" | %{

  # Define what each job does
  $ScriptBlock = {
    param($server)
    Write-Host "[processing '$server' inside the job]"
    $app = Get-WmiObject -Class Win32_Product -computer $server -Filter "Name = 'Java 7 Update 60'"
    $app.Uninstall()
    Start-Sleep 60
  }

  Write-Host "processing $_..."

  # Execute the jobs in parallel
  Start-Job $ScriptBlock -ArgumentList $_
}

Get-Job

While (Get-Job -State "Running")
{
  Start-Sleep 10
}

Get-Job | Receive-Job