我尝试使用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 "[email protected]" -From "[email protected]" -Subject "Some Server Down" -SmtpServer "XXX.XXX.com" -Attachments "C:\XXXX\Check_Server\output.txt"
}
Else
{
Send-MailMessage -To "[email protected]" -From "[email protected]" -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
非常感谢!
我试着用于不同的目的,我认为它也会对你有所帮助
cmd / c“start powershell.exe -f \ path *******。ps1”