Author Posts

March 8, 2016 at 6:17 am

Hi, I did the following:

`
$trigger = New-JobTrigger -Once -At '15:00 pm'
$cred = Get-Credential -UserName adminUser -Message "Enter password for given account"
Register-ScheduledJob -Name Test -FilePath d:\PS\DiskSpaceReport.ps1 -Trigger $trigger -Credential $cred
`

Scheduled job worked fine (I received email with DiskSpaceReport in a form of html report) but `Get-Job` returned:

State: Completed
HasMoreData: False

Because of this `Receive-Job -id JobID` returns nothing. It should have returned messages that DiskSpaceReport.ps1 produces when run directly – not as job. I know that in case of scheduled jobs the results remain on disk after being received even without using -Keep parameter of Receive-Job. In my case I have no results alltogether. As soon as job was successfully completed HasMoreData was False.

March 8, 2016 at 6:18 am

How does the script produce its output? Write-Host? A Format command?

March 8, 2016 at 6:23 am

That's why I keep telling people not to use that :). Host displays can't be captured. Use Write-Output.

March 8, 2016 at 6:37 am

Ok, my mistake in a hurry :-). I started to use scheduled jobs yesterday because they are ideal for reporting purposes on daily basis. So in this case if I would have, for instance, one Write-Output line then HasMoreData would be True and Receive-Job -id JobId would display only what is written by Write-Output.

March 8, 2016 at 6:40 am

Correct. Only pipeline output is capture-able, and Write-Output writes to the pipeline. In theory, the -Verbose, -Error, and -Warning pipes are also captured, albeit to separate locations, but I'm not sure if that's implemented with scheduled jobs, since those persist as deserialized XML on disk.

March 8, 2016 at 6:46 am

That is something I will surely run into. If I have Write-Verbose inside script these messages would be visible if I run script with -Verbose switch. If I want to see all these messages with Receive-Job does it mean I would have to register scheduled job with -FilePath parameter and (PathToPS1File -Verbose)? What about Write-Warning in script – will these messages be displayed with Receive-Job?

March 9, 2016 at 4:42 am

Check whether any data saved in below path which is the default path used by the register-schedulejob

$home\AppData\Local\Microsoft\Windows\PowerShell\ScheduledJobs\\Output\

When you use receive-job use always use -keep parameter otherwise it will deleted the results from the disk.

March 9, 2016 at 4:51 am

Receive-Job does not need -Keep parameter in case of scheduled jobs, when it comes to "ordinary" background jobs -Keep parameter is needed.

Speaking of Write-Verbose in script which is executed as background job I did some tests and if Write-Verbose -Message SomeMessage -Verbose is in the script then verbose message will be displayed with Receive-Job. I guess there is some more elegant way of achieving this than using -Verbose switch in each Write-Verbose line in script.