0

Is there anyway of using ps to get the pid of a powershell command which is already executing.

For e.g., say I've already executed the following command.

ls | ? { <where condition> } | % { <some operations> }

The goal is I open up another powershell window and type in something that allows me to wait for the above to complete.

Any ideas?

wp78de
  • 1,662
  • 10
  • 25
deostroll
  • 1,795
  • 7
  • 24
  • 37
  • 1
    No. You can't wait for that. Have a look at PowerShell Jobs, those might be helpful. – Seth Sep 12 '17 at 11:58

1 Answers1

1

allow me to wait for the above to complete.

In that command you are using a ForEach-Object (%).

The ForEach-Object has a default argument -Process that accepts the script block you are providing that is doing the processing for each item in the pipeline. But that commandlet also offers a -End {Scriptblock} argument that will be executed after all the pipeline input has been accepted and processed. You could use this -End block to send your notification somehow. Perhaps by writing a file or sending an email or whatever else you wanted to notify you.

ls | ? { <where condition> } | % { <some operations> } -End { #alert me!}
Zoredache
  • 19,828
  • 8
  • 50
  • 72