Powershell 스크립트를 예약된 작업으로 실행하는 데 문제가 있습니다. 스크립트는 두 개의 Hyper-V 호스트에 원격으로 로그인하여 복제 상태를 쿼리하고 결과를 이메일로 다시 보냅니다. Powershell ISE에서 수동으로 실행하거나 스크립트를 직접 실행하면 스크립트가 제대로 작동하지만, 예약된 작업으로 실행하면 작업이 실행 상태에서 멈춰 결과를 얻지 못합니다.
스크립트를 단순히 로컬 폴더에 텍스트 파일을 쓰는 스크립트로 대체하여 예약된 작업 설정이 올바르게 작동하는지 확인했습니다. 또한 스크립트를 수동으로 실행할 때 작업이 실행되는 것과 동일한 사용자로 로그인되어 있으므로 그게 아닙니다. 무엇을 놓치고 있습니까?
내 스크립트는 다음과 같습니다.
$array = @("host1.domain.com", "host2.domain.com")
for ($i=0; $i -lt $array.length; $i++) {
$pass = cat C:\Scripts\Creds.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "username",$pass
Invoke-Command -ComputerName $array[$i] -Credential $mycred -FilePath "C:\Scripts\Check_VMReplication.ps1"
}
스크립트는 동일한 폴더에 있는 다른 스크립트를 호출합니다.
$hstname = Hostname
$Replication = Get-VMReplication
$MessageFail = $hstname + ' Replication Alert'
$SmtpServer = 'smtp.server.com'
hostname > C:\Scripts\iveremoted.txt
for ($i=0; $i -lt $Replication.length; $i++) {
$MessageBody = $hstname+ " has reported a replication status of " + $Replication.health[$i] + ' for server ' + $Replication.name[$i]
$FailMessageSubject = $Replication.name[$i] + " Replication Alert"
if ($Replication.health[$i] -ne 'Normal') {
send-mailmessage -to "[email protected]>" -from '[email protected]' -subject $MessageFail -body $MessageBody -smtpServer $SmtpServer
}
else{
send-mailmessage -to "[email protected]" -from '[email protected]' -subject 'Everything's OK' -body $MessageBody -smtpServer $SmtpServer
}
}
iveremoted.txt 파일이 원격 시스템에 기록되지 않기 때문에 스크립트가 로그온할 수 없는 것으로 나타납니다.
내가 무엇을 놓쳤을지 알 수 있나요?
답변1
이유는 모르겠지만 다음을 사용하여 Check_VMReplication.ps1 코드를 실행하면
Invoke-Command -Credential $mycred -ScriptBlock {CODE GOES HERE}
두 번째 스크립트 파일 대신 잘 실행됩니다. 그냥 그렇게 해야 할 것 같아요.