Issue with Wait-RSjob
chadwestes opened this issue · comments
Perhaps I am just misunderstanding the way this should work. This is basically what my code looks like:
$datatable = Invoke-Sqlcmd2 -ServerInstance MyServer -Database MyDatabase -Query @" SELECT ID, SourceServer, DbName, DestinationServer FROM myTable WHERE ID in (1,2,3,4,5) "@ [void]$(($datatable.Rows)|Start-RSJob -Name {"$($_.ID)-$($_.DbName)"} -Throttle 10 -ScriptBlock { #Really long complicated script block which restores, manipulates and backups up databases }) [void] $(Get-RSJob | Wait-RSJob) [void] $(Get-RSJob | Stop-RSJob) [void] $(Get-RSJob | Remove-RSJob) $MailMessage = new-object System.Net.Mail.MailMessage $MailMessage... #Fill in appropriate properties for the mail message object $smpt = new-object Net.Mail.SmtpClient("smtp.server.com") $smtp.Send($MailMessage)
When I run my code from PS ISE, it runs all of the jobs for each of the 5 databases I feed it before sending the email which includes information on the success/failure of each job. When I run this script from a SQL Agent job as either a powershell or a call from the Operating System (CmdExec), it only waits for the first job to complete/error before sending the email which shows one job finished and 4 running. The server only shows one powershell instance running, but none of the other jobs ever finish. The same thing happens when run from a Scheduled Task. Any clue why Wait-RSJob does not get respected by TaskScheduler or SQLAgent?
Did you try to just { write-host "number $_" }
instead of you long scriptblock ?
may be there is some exception inside ?
which version of module you use, what commands inside your sriptblock you use ?
if you use invoke-sqlcmd inside you scriptblock, may be invoke-sqlcmd incompatible with poshrsjob (see #173)
Good call MVKozlov. Someone went and encrypted a database then turned encryption off. I was checking for encryption status but not the existence of a database key. Apparently, you can't restore an unencrypted database without the certificate if the database key exists.