【发布时间】:2018-02-10 14:07:25
【问题描述】:
我尝试使用 PowerShell 脚本检查所有服务器是否启动或关闭。脚本如下:
## Test Server up or down ##
## Start-Transcript -path C:\XXXX\Check_Server\output.txt -force
$names = Get-content "C:\XXXX\Check_Server\AllSQLHostName.txt"
Start-Transcript -path C:\XXXX\Check_Server\output.txt -force
foreach ($name in $names){
if (Test-Connection -ComputerName $name -Count 1 -ErrorAction SilentlyContinue){
Write-Host "$name,up"
}
else{
Write-Host "$name,down"
}
}
Stop-Transcript
Start-Sleep -s 5
If (Select-String -Pattern "Down" -Path C:\XXXX\Check_Server\output.txt)
{
Send-MailMessage -To "BBBB@XXXX.com" -From "AAAA@XXXX.com" -Subject "Some Server Down" -SmtpServer "XXX.XXX.com" -Attachments "C:\XXXX\Check_Server\output.txt"
}
Else
{
Send-MailMessage -To "BBBB@XXXX.com" -From "AAAA@XXXX.com" -Subject "All Server Up" -SmtpServer "XXX.XXX.com" -Attachments "C:\XXXX\Check_Server\output.txt"
}
当我手动运行脚本时,它会向我发送“All Server Up”。但在任务调度程序中,它向我发送“Some Server Down”。我仔细检查了 output.txt。没有服务器停机。 BTW:任务调度器中的设置是:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -file C:\XXXX\Check_Server\01_Server_Test_Server_up_or_down.ps1
非常感谢!
【问题讨论】:
-
听起来您的计划任务有问题。它是否具有与您手动运行时相同的权限?
-
您在哪个帐户下运行任务?系统帐户没有网络访问权限。您需要使用网络服务帐户。
-
什么用户在任务调度器中运行脚本?
-
即使我点击“以最高权限运行”为“social.technet.microsoft.com/Forums/lync/en-US/…”我仍然遇到同样的错误。
-
谢谢大家,他们以相同的用户身份运行。谢谢
标签: powershell scheduled-tasks