Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating batch jobs in PowerShell

Imagine a DOS style .cmd file which is used to launch interdependent windowed applications in the right order.

Example:
1) Launch a server application by calling an exe with parameters.
2) Wait for the server to become initialized (or a fixed amount of time).
3) Launch client application by calling an exe with parameters.

What is the simplest way of accomplishing this kind of batch job in PowerShell?

like image 921
d91-jal Avatar asked Sep 08 '08 10:09

d91-jal


People also ask

Can you use batch in PowerShell?

To run the batch commands from PowerShell, we can use the Start-Process cmdlet as earlier explained and which executes a cmd command on the server.

What is batch cmdlets in PowerShell?

Batch cmdlets use the PowerShell pipeline to send data between cmdlets. This has the same effect as specifying a parameter, but makes working with multiple entities easier. For example, find and display all tasks under your account: PowerShell Copy.


1 Answers

Remember that PowerShell can access .Net objects. The Start-Sleep as suggested by Blair Conrad can be replaced by a call to WaitForInputIdle of the server process so you know when the server is ready before starting the client.

$sp = get-process server-application
$sp.WaitForInputIdle()

You could also use Process.Start to start the process and have it return the exact Process. Then you don't need the get-process.

$sp = [diagnostics.process]::start("server-application", "params")
$sp.WaitForInputIdle()
$cp = [diagnostics.process]::start("client-application", "params")
like image 120
Lars Truijens Avatar answered Sep 28 '22 04:09

Lars Truijens