Jobs in Powershell


This post serves more as a reference that an introduction to Powershell Jobs.

  • Cmdlets are: Get-Job, Receive-Job, Remove-Job, Resume-Job, Start-Job, Stop-JobSuspend-Job and Wait-Job
    but other commands have -AsJob parameters (Get-WMIObject, Invoke-Command, CDXML snippets)
  • Help can be found in the following topics:
    • about_Jobs
    • about_Job_Details
    • about_Remote_Jobs
    • about_Scheduled_Jobs
    • about_Scheduled_Jobs_Advanced
    • about_Scheduled_Jobs_Basics
    • about_Scheduled_Jobs_Troubleshooting
  • Suspend-Job and Resume-Job are only available with workflows
  • Receive-Job deletes the data it sends to you except if you use -Keep as a parameter
  • PSJobTypeName can be
    • BackgroundJob (regular Start-Job),
    • WMIJob (Get-WmiObject -AsJob),
    • CimJob (use CDXML files, not Get-CimInstance),
    • PSScheduledJob (Register-ScheduledJob)
    • PSWorkflowJob (nameofmyworkflow -AsJob)
  • As a job is run in another Windows Powershell process than yours, you cannot use it with cmdlets such as Register-WMIEvent
  • Start-Job runs the command locally (but that command may perform remote connections), whereas Invoke-Command runs the command remotely and you get the response back.
  • If you use parameter(s), you must use ArgumentList to pass them around as jobs are run in separate Powershell instances and processes (unsecapp for Get-WMIObject,  wsmprovhost for Invoke-Command)
  • Sessions (via Enter-PSSession or New-PSSession) are a way to maintain that remote connection when you have multiple script blocks to run on a target machine
  • ScheduledTasks is a CDXML (WMI Wrapper) module about the TaskScheduler, whereas PSScheduledJob manages the Powershell Scheduled Jobs which are… tasks found under \Microsoft\Windows\Powershell\ScheduledJobs
  • ScheduledJobs are primarily maintained with Register-ScheduledJob, but to work with result you must import explicitly the module and you can only see the jobs that you create/manage in your context whose results are stored in %AppData%\Local\Microsoft\Windows\Powershell\ScheduledJobs\<JobName>\Output
  • If you run jobs inside workflows, you lose the result because they are still run in an external process to yours
  • Some snippets:

     

Leave a comment

Your email address will not be published. Required fields are marked *