How to run multipe commands by using multiple ps-sessions

Welcome Forums General PowerShell Q&A How to run multipe commands by using multiple ps-sessions

This topic contains 2 replies, has 2 voices, and was last updated by

Participant
6 months, 2 weeks ago.

• Author
Posts
• #113677

Participant
Topics: 2
Replies: 1
Points: 0
Rank: Member

Hi Team,
I have a requirement where we have to run multiple commands at a time and each command should use separate PowerShell session.
For example, I have 100 commands in a text file, the PowerShell should read the content of the text file and run all commands parallel.  If we run normally, the commands are running sequential, the second line will run only after completing the first line. In my environment, each command is taking more than 2 days. If we run all commands parallel then the script will complete within 3 days.
The scripts look like below
C:\ASC_Tools\emcopy64.exe "C:\Test\Source\text1" "C:\Test\Dest\text" /s /sdd /de /r:3 /w:3 /c /nosec /cm md5 /th 3 /f /tee /log+:"C:\test\test.log"
C:\ASC_Tools\emcopy64.exe "C:\Test\Source\text2" "C:\Test\Dest\text2" /s /sdd /de /r:3 /w:3 /c /nosec /cm md5 /th 3 /f /tee /log+:"C:\test\test1.log"

Appreciate all help with this.

How to run multipe commands by using multiple ps-sessions

• #113729

Participant
Topics: 0
Replies: 13
Points: 9
Rank: Member

There are 2 ways to accomplish this. It's a common problem that admins need to solve.

The first is the way you're asking, which I don't believe is the best way. But it's often the way we think of first because we've boxed ourselves into a solution based on prior learning. If I was writing a batch file, I'd just use a bunch of start statements to launch new windows. In PowerShell, the equivalent looks more like this.

start-process powershell.exe -argument "-noexit -nologo -noprofile -command Get-Process"
start-process powershell.exe -argument "-noexit -nologo -noprofile -command Get-Service"


The challenge with this is once you've spun off those new processes, you can't really collect information from them anymore. The output they generated is written to those other windows and those windows stay open because of the -noexit parameter. Without the -noexit they disappear and the output generated is lost forever.

The second was is really the way you should be running these commands, and that is to use a job. PowerShell allows you to run any command in the background as a job. You hit enter and the command starts running but your window comes back to a blinking cursor. You can use get-job to check the status and receive-job to view the results. Using jobs is awesome in scripts because I can kick off a long running command, go do some other stuff, and then have my script pause until that command is complete before continuing.

invoke-command -AsJob -ScriptBlock {Get-Process}
invoke-command -AsJob -ScriptBlock {Get-Service}


In either case, you can obviously change what command is in the output (unless all you really needed were a Get-Process and a Get-Service), and in your case probably want it to be a variable that you pulled from your file.

• #113816

Participant
Topics: 2
Replies: 1
Points: 0
Rank: Member

Awesome, It is working for me. Thank you very much for your quick reply.

The topic ‘How to run multipe commands by using multiple ps-sessions’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort