![远程重启否则不执行任何操作-powershell](https://linux22.com/image/714166/%E8%BF%9C%E7%A8%8B%E9%87%8D%E5%90%AF%E5%90%A6%E5%88%99%E4%B8%8D%E6%89%A7%E8%A1%8C%E4%BB%BB%E4%BD%95%E6%93%8D%E4%BD%9C-powershell.png)
我正在尝试从文本文件重新启动计算机列表。首先测试计算机是否已启动。这看起来很简单,但就是无法正常工作。
我看不出我错在哪里
$comp = Get-Content C:\temp\srv.txt
$s = Get-Credential
foreach ($comps in $comp)
{
{
if(-not(Test-Connection -ComputerName $comp -Count 1 ))
{
Write-Host "$comp unavailable"
}
}
else
{
Restart-Computer -ComputerName $comp -force -Credential $s
}
}
提前感谢您的帮助:)
答案1
你已经接近了,只是代码中多了一些不需要的括号。
$comp = Get-Content C:\temp\srv.txt
$s = Get-Credential
foreach ($comps in $comp)
{
if(-not(Test-Connection -ComputerName $comp -Count 1 ))
{
Write-Host "$comp unavailable"
}
else
{
Restart-Computer -ComputerName $comp -force -Credential $s
}
}