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...

4

1 回答 1

1

这是方向,根据您的特定需求进行调整...添加计时器等...

$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
}

它将为每个服务器创建一个带有 ServerName 的作业,要获取作业状态/信息,请使用:

Get-Job | Receive-Job
于 2016-03-07T13:02:04.883 回答