1

i'm using windows server 2003, now i have a script to restart one specific service, can someone guide me how to run this script in parallel? i already use start-job, but seems it only run in backround but still sequential, below is my script:

file name: restart-service.ps1

$start = Get-Date
$Serverlist = Get-Content -path C:\Computers.txt

ForEach ($Server in $Serverlist){
start-job -scriptblock {param($Server) Restart-Service -InputObject $(Get-Service -ComputerName $Server -Name "aspnet_state")} -ArgumentList $Server
}
Get-Job | Wait-Job
Get-Job | Receive-Job
$end = Get-Date
$timespan = $end - $start
$seconds = $timespan.TotalSeconds
Write-Host "This took me $seconds seconds."

or this

$start = Get-Date
$Server = Get-Content -path C:\Computers.txt

$scriptblock = {
    Param($Server)
    Restart-Service -InputObject $(Get-Service -ComputerName $Server -Name "aspnet_state")
}
$Server | % {Start-Job -Scriptblock $scriptblock -ArgumentList $_ | Out-Null}
Get-Job | Wait-Job | Receive-Job

$end = Get-Date
$timespan = $end - $start
$seconds = $timespan.TotalSeconds
Write-Host "This took me $seconds seconds."

but i only can see the result, but can not see any result related information of my server's IP, can someone help me how to improve it...

1 Answer 1

1

This is the Direction, tweak it for your specific needs... add timer etc...

$Serverlist = Get-Content -path C:\Computers.txt

Foreach ($Server in $Serverlist)
{
    Start-Job -Name "$Server-Job" -ScriptBlock {
    Get-Service -ComputerName $args[0] -Name "aspnet_state" | Restart-Service 
    } -ArgumentList $Server
}

It will Create a job with the ServerName for each server, to Get the Job status/info, use:

Get-Job | Receive-Job
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.