Finished scheduled job - false HasMoreData

This topic contains 8 replies, has 3 voices, and was last updated by Profile photo of Bojan Zivkovic Bojan Zivkovic 9 months ago.

  • Author
    Posts
  • #36252
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    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.

  • #36253
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #36254
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    Write-Host

  • #36255
    Profile photo of Don Jones
    Don Jones
    Keymaster

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

  • #36257
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    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.

  • #36259
    Profile photo of Don Jones
    Don Jones
    Keymaster

    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.

  • #36260
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    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?

  • #36309
    Profile photo of Nagendra Narembally
    Nagendra Narembally
    Participant

    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.

  • #36310
    Profile photo of Bojan Zivkovic
    Bojan Zivkovic
    Participant

    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.

You must be logged in to reply to this topic.