我尝试使用PowerShell脚本检查所有服务器是启动还是关闭。脚本如下:
else
当我手动运行脚本时,它会发送给我" All Server Up"。但是在任务调度程序中,它会向我发送一些服务器关闭"。我仔细检查了output.txt。没有服务器关闭。 BTW:任务调度程序中的设置是:
## 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"
}
非常感谢!
答案 0 :(得分:0)
我出于不同的目的尝试了此操作,我认为这也会对您有帮助
cmd / c“启动powershell.exe -f \ path *******。ps1”