使用 powershell 脚本重启机器
Restart machine using powershell script
我正在 运行 宁以下代码,但重新启动不起作用。我的意图是 运行 同时在所有远程机器上并行重启命令。
$YourFile = gc "machinelst.txt"
$username = "user1"
$password = "pass1"
$secpw = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $secpw)
foreach ($computer in $YourFile)
{
Invoke-Command -ComputerName $computer -credential $cred -ErrorAction Stop -ScriptBlock { Restart-Computer -ComputerName $computer -Force } -AsJob
}
这看起来像是 Get-Job 的输出 - 你能试试 Receive-Job $id (Receive-Job 80) 吗?
应该给你实际的异常。
试试这个(如果可行,您可以添加 -asjob):
$username = "yourdomain\user1"
$password = ConvertTo-SecureString "pass1" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
get-content "machinelst.txt" | %{
Restart-Computer -ComputerName $_ -Authentication default -Credential $cred
}
如果你想使用工作,你可以这样做:
$listjob=@()
get-content "machinelst.txt" | %{
$listjob+=Restart-Computer -ComputerName $_ -Authentication default -Credential $cred -AsJob
}
$listjob | Wait-Job -Timeout 30
$listjob | %{
if ($_.State -eq 'Failed' )
{
Receive-Job -Job $_ -Keep
}
}
这可能是并行运行的,就像 invoke-command 对一组计算机名称所做的那样:
restart-computer computer01,computer02,computer03,computer04,computer05
或者这个。 winrm 服务需要几分钟才能恢复,但它们似乎同时重新启动。
$c = get-credential
$list = 1..10 | % tostring computer00
restart-computer $list -wait -protocol wsman -cr $c
我正在 运行 宁以下代码,但重新启动不起作用。我的意图是 运行 同时在所有远程机器上并行重启命令。
$YourFile = gc "machinelst.txt"
$username = "user1"
$password = "pass1"
$secpw = ConvertTo-SecureString $password -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ($username, $secpw)
foreach ($computer in $YourFile)
{
Invoke-Command -ComputerName $computer -credential $cred -ErrorAction Stop -ScriptBlock { Restart-Computer -ComputerName $computer -Force } -AsJob
}
这看起来像是 Get-Job 的输出 - 你能试试 Receive-Job $id (Receive-Job 80) 吗?
应该给你实际的异常。
试试这个(如果可行,您可以添加 -asjob):
$username = "yourdomain\user1"
$password = ConvertTo-SecureString "pass1" -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
get-content "machinelst.txt" | %{
Restart-Computer -ComputerName $_ -Authentication default -Credential $cred
}
如果你想使用工作,你可以这样做:
$listjob=@()
get-content "machinelst.txt" | %{
$listjob+=Restart-Computer -ComputerName $_ -Authentication default -Credential $cred -AsJob
}
$listjob | Wait-Job -Timeout 30
$listjob | %{
if ($_.State -eq 'Failed' )
{
Receive-Job -Job $_ -Keep
}
}
这可能是并行运行的,就像 invoke-command 对一组计算机名称所做的那样:
restart-computer computer01,computer02,computer03,computer04,computer05
或者这个。 winrm 服务需要几分钟才能恢复,但它们似乎同时重新启动。
$c = get-credential
$list = 1..10 | % tostring computer00
restart-computer $list -wait -protocol wsman -cr $c