0

I'm new to Powershell and I found it difficult to tell my first powershell commandline (powershell_1) to : start a new powershell commandline (powershell_2) & let it (the powershell_2) execute my multiline script (written under powershell).

My script is :

$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop;
$wshell.Popup("Are you looking at me?",0,"Hey!",48+4);

The code I'am using to launch the first powershell commandline and set things to work as intended

    Process powershell_1 = new Process();
    powershell_1.StartInfo.FileName = "powershell";

    string argPowershell_2 = "help";

    powershell_1.StartInfo.Arguments = "Start-Sleep -Milliseconds 500; start powershell -argumentlist \" "+ argPowershell_2 + " \"";
    MessageBox.Show(powershell_1.StartInfo.Arguments);

    powershell_1.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    powershell_1.Start();

In the example above, I could tell the 2nd commandline to execute help in the second one. I'm looking for a good way to escape quotes problems and tell it in a correct manner how to execute myscript instead.

EDIT

Ps : I'm not trying to pass a path of a someScript.ps to the second powershell commandline. I want to pass it literally, in multiline format.

4
  • 1
    but why? just use scriptblock and jobs Commented Aug 28, 2018 at 18:28
  • Possible duplicate of Call PowerShell script PS1 from another PS1 script inside Powershell ISE Commented Aug 28, 2018 at 18:45
  • You can use Invoke-Expression and pass it the path to the script you want to run, along with parameters (if that's what you're trying to do). Commented Aug 28, 2018 at 18:46
  • I edited my post. I don't want to store the script on a .ps file. Commented Aug 28, 2018 at 21:32

2 Answers 2

0

I don't know if is the right manner, but you can do the same with the code below

$argPowershell_2 = "help";
Start-Process -Wait powershell -ArgumentList "Start-Sleep -Milliseconds 500; start powershell -argumentlist $argPowershell_2" 
Sign up to request clarification or add additional context in comments.

2 Comments

That works possibly for help command. But it doesn't run something like start-sleep 1;'hi';start-sleep 1;'hello'
Also, note that the OP is calling PowerShell from C#.
0

Solution

The argument to pass to my powershell_1 process in my case could be :

start powershell -argumentlist "-noexit","start-job -scriptblock { help; start-sleep 1;}

Using this line of code, and by tracking the background job in the new powershell process I got the following result :

(get-job).childjobs | select * to track job results

If you want, inside the scriptblock to run something like (declaring variables, defining/calling custom functions, ... etc) :

$a = new-object System.net.webclient;
$a.downloadfile('https://www.stackoverflow.com','d:\file');

you need to escape the dollar sign ($) as well as any other double quote (") using the ` char.

start powershell -argumentlist "-noexit","start-job -scriptblock { `$a = new-object system.net.webclient; `$a.downloadfile('https://www.stackoverflow.com','d:\file');}

More details about background jobs are in the link below. This solves my problem. Thank you all.

PowerShell: Start-Job -scriptblock Multi line scriptblocks?

1 Comment

For your answer to benefit future readers, please post a working example and ideally also explain why your original question was an instance of a XY problem.

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.