Powershell: Capture process id of a background job

daniel

I want to start a background job and capture it's process id into a .pid file. I was able to do it with the Start-Process as follows:

Start-Process C:\process.bat -passthru | foreach { $_.Id } > start.pid

Now, I want to wrap Start-Process with Start-Job, to run it in the background, like this:

$command = "Start-Process C:\process.bat -passthru | foreach { $_.Id }"
$scriptblock = [Scriptblock]::Create($command)
Start-Job -ScriptBlock $scriptblock

Unfortunatelly, this doesn't work and Receive-Job gives me the following error:

The term '.Id' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
+ CategoryInfo          : ObjectNotFound: (.Id:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
+ PSComputerName        : localhost

Looks like it's something wrong with the $_ variable. Maybe it gets overwritten by the Start-Job.

Any clues greatly welcome!

boeprox

That is because the variable is being expanded when using double quotes. If you want to keep the $_, then you need to use single quotes.

$command = 'Start-Process C:\process.bat -passthru | foreach { $_.Id }'
$scriptblock = [Scriptblock]::Create($command)
Start-Job -ScriptBlock $scriptblock

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to capture PID of a background process

From Dev

Capture job id of a job submitted by qsub

From Dev

Flask - job not running as a background process

From Dev

Restart a completed background job in PowerShell

From Dev

Start a detached background process in PowerShell

From Dev

How to capture process output asynchronously in powershell?

From Dev

.NET DNS class powershell background job possible?

From Dev

PowerShell Background-Job Processing of a while loop

From Dev

Getting process id of background process in Perl

From Dev

background process catching process id before termination

From Dev

Create background process and get process ID

From Dev

Unable to capture process id from $! in bash 3.2.57

From Dev

command to capture exit status of last background process in bash

From Dev

Node.JS Job / Background Process and high availability

From Dev

Run while loop until background process finish its job

From Dev

Eclipse Background Job: Process specific calls in the Main thread?

From Dev

Spawning process in background on Jenkins - job that won't stay in queue

From Dev

Run a background job in a different process as the user who queued it

From Dev

How to check if a background job has timed out in powershell

From Dev

What's the easiest way to capture a process ID and kill it?

From Dev

In a single terminal window capture the command to start a.out 3 times each running as background job

From Dev

How do I view the job queue in lftp after it has moved to a background process?

From Dev

Wait for background processes to finish with inverse match process ID

From Dev

Last background process ID ($!) not working in sudo bash command

From Dev

Why is $! returning the wrong process ID for a subshell running in background?

From Dev

How to capture PowerShell Write-Progress text in child spawn process in node.js?

From Dev

Capture Verbose Stream from Job

From Dev

powershell v2 - how to get process ID

From Dev

Using powershell to identify process ID, Name, and arguments used to start

Related Related

  1. 1

    How to capture PID of a background process

  2. 2

    Capture job id of a job submitted by qsub

  3. 3

    Flask - job not running as a background process

  4. 4

    Restart a completed background job in PowerShell

  5. 5

    Start a detached background process in PowerShell

  6. 6

    How to capture process output asynchronously in powershell?

  7. 7

    .NET DNS class powershell background job possible?

  8. 8

    PowerShell Background-Job Processing of a while loop

  9. 9

    Getting process id of background process in Perl

  10. 10

    background process catching process id before termination

  11. 11

    Create background process and get process ID

  12. 12

    Unable to capture process id from $! in bash 3.2.57

  13. 13

    command to capture exit status of last background process in bash

  14. 14

    Node.JS Job / Background Process and high availability

  15. 15

    Run while loop until background process finish its job

  16. 16

    Eclipse Background Job: Process specific calls in the Main thread?

  17. 17

    Spawning process in background on Jenkins - job that won't stay in queue

  18. 18

    Run a background job in a different process as the user who queued it

  19. 19

    How to check if a background job has timed out in powershell

  20. 20

    What's the easiest way to capture a process ID and kill it?

  21. 21

    In a single terminal window capture the command to start a.out 3 times each running as background job

  22. 22

    How do I view the job queue in lftp after it has moved to a background process?

  23. 23

    Wait for background processes to finish with inverse match process ID

  24. 24

    Last background process ID ($!) not working in sudo bash command

  25. 25

    Why is $! returning the wrong process ID for a subshell running in background?

  26. 26

    How to capture PowerShell Write-Progress text in child spawn process in node.js?

  27. 27

    Capture Verbose Stream from Job

  28. 28

    powershell v2 - how to get process ID

  29. 29

    Using powershell to identify process ID, Name, and arguments used to start

HotTag

Archive