首页 > 解决方案 > 使用 powershell 脚本重启机器

问题描述

我正在运行以下代码,但重新启动不起作用。我的意图是一次在所有远程机器上并行运行重启命令。

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

在此处输入图像描述

标签: powershellpowershell-2.0powershell-3.0powershell-4.0

解决方案


这看起来像是 Get-Job 的输出 - 你可以试试 Receive-Job $id (Receive-Job 80)。

应该给你实际的例外。


推荐阅读