I have another problem, which I can't solve by myself even using the search..
I have a script that starts Robocopy as job, and a second job that watches this script if it's running, if not, send an e-mail. Now I want to add in the watch script part to start the whole script again. (loop)
& "$PSScriptRoot\Sync_Start&Watch.ps1"
Robocopy-Job:
Script_Block_Sync = {
param ($rc_logfile, $rc_source, $rc_destination)
robocopy $rc_source $rc_destination /MIR /SEC /SECFIX /COPYALL /m /r:5 /Mon:1 /Mot:1 /unilog:$rc_logfile
}
Start-Job -Name Robocopy_Sync -ScriptBlock $Script_Block_Sync -ArgumentList $rc_logfile, $rc_source, $rc_destination
Watch_Job:
$Script_Block_Check = {
param($MailParams, $mailbody_error, $PSScriptRoot)
while ((Get-Process Robocopy).Responding) {Start-Sleep -Seconds 30}
if (!(Get-Process Robocopy).Responding) {
Send-MailMessage @MailParams -body $mailbody_error
& "$PSScriptRoot\Sync_Start&Watch.ps1"
}
}
Start-Job -Name Robocopy_Check -ScriptBlock $Script_Block_Check -ArgumentList $MailParams, $mailbody_error, $PSScriptRoot
I've tried with $PSScriptRoot, with the full path and with separate $script variable. If I run only the line (F8) or the whole IF block (F8) the script starts running. If it's not possible to start another script, maybe another job which starts the script?
Any idea what I missed, or is it still not possible?
Thank you for any help!
Best regards
After a week of searching and trying multiple variations I found a solution to get the wanted loop. Not what I actually want, but it works. If someone read this here and have another or even better idea, you're very welcome! My idea was, to start a script via Task Scheduler, that starts everything in background without showing "Running" state.
FYI: To my problem, I missed the point, that PS jobs only run in the session they started in, so everything works fine if I test the script in PS ISE, but not via Task Scheduler, because if the script (session) ends, the jobs started within the script ends too.
So to get a loop, I use the following code:
while (!(Get-Process Robocopy).Responding) {
$Script_Block {...}
Start-Job -Name Robocopy -ScriptBlock $Script_Block
Start-Sleep -Seconds 5
while ((Get-Process Robocopy).Responding) {
Start-Sleep -Seconds 60
}
Send-MailMessage ...
}