user829174
user829174

Reputation: 6362

Run a PowerShell method from cmd and don't wait for its return

In cmd I'm trying to run Method1 which is in a PowerShell script, script1. Method1 is a method that takes a few hours, and I simply want to fire and forget.

The following is working for me:

c:\temp> powershell
PS c:\temp> . .\script1.ps1;Method1

When I do the lines above, everything is working fine as long as I keep the CMD of PS opened. once I close the PS window, it kills Method1.

So actually I want that from cmd, in one line, to somehow make Method1 work without the dependency of the PowerShell window, maybe create a new process.. I am not really sure.

I've tried:

c:\temp> cmd /c powershell . .\script1.ps1;Method1

It is running for a few seconds, but when the cmd gets closed, then Method1 also terminates.

I also tried

c:\temp>cmd /c powershell -noexit "& { . .\script.ps1;Method1 }"

Again, once I do this, it is working. However, a PowerShell window is opened and if I close it then it terminates Method1.


From you help, I've tried:

c:\temp> cmd /c powershell start-process cmd /c powershell . .\script1.ps1;Method1

But I get an exception:

Start-Process : A positional parameter cannot be found that accepts argument 'powershell'.

But still, I am not able to make it work.

Upvotes: 4

Views: 24367

Answers (3)

Patrick Burwell
Patrick Burwell

Reputation: 173

Easy answer ya'll; Just paste "start" command into your PS window (whether in a remote session or not) and it works fine:

Start C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -file 'driverletter:\path\yourpowershellscript.ps1'

Upvotes: 1

4c74356b41
4c74356b41

Reputation: 72151

You can use PowerShell jobs for that, so just:

Start-Job -FilePath somepath

And add a method call at the end of the script, or pass in a Scriptblock like:

Start-Job -ScriptBlock {. .\path_to_ps1; Method1}

Or perhaps use the hackish:

start-process cmd -WindowStyle Hidden -ArgumentList "'/c powershell . .\script1.ps1;Method1'"

Actually, you can just launch PowerShell, without CMD, and I am not sure why I was using a cmd approach:

start-process powershell -WindowStyle Hidden -ArgumentList ". .\script1.ps1;Method1"

Upvotes: 3

Deadly-Bagel
Deadly-Bagel

Reputation: 1620

Alternatively if you want a pure PowerShell solution (note this needs to be running as Admin):

Invoke-Command LocalHost -Scriptblock $script -InDisconnectedSession

The InDisconnectedSession switch runs it in a separate PowerShell session that will not be terminated when you close the PowerShell window. You can also use Get-PSSession and pass the session to Enter-PSSession to interact with it during or after execution. Remember in this state if you close the window it -will- kill it, so you'll want to use Exit-PSSession to keep it alive.

There is however a problem - you can't do any remoting tasks, at least not easily. This incurs the wrath of the "double hop" where you remote to one computer (your own in this case), then to another, and for security PowerShell refuses to pass any credentials to the second remoting session so it can't connect, even if you put the credentials in manually. If you do need to do remoting I recommend sticking with launching a hidden PowerShell process.

Upvotes: 4

Related Questions