4

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.

0

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Who mentioned Invoke-Expression?
3

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"

4 Comments

I'm looking at your last answer, I'm trying to run it from a cmd, so how do I invoke the start-process
i'm not sure what you are asking, just paste this into powershell window?
Once I paste to PS I get error: Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
Start-Job won't work, when you close the PowerShell window any associated jobs and threads will be closed with it. You need to either use sessions or a hidden new PS process.
1

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.