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.

mklement0
  • 382,024
  • 64
  • 607
  • 775
alyncibi
  • 13
  • 4
  • 1
    but why? just use scriptblock and jobs – 4c74356b41 Aug 28 '18 at 18:28
  • Possible duplicate of [Call PowerShell script PS1 from another PS1 script inside Powershell ISE](https://stackoverflow.com/questions/6816450/call-powershell-script-ps1-from-another-ps1-script-inside-powershell-ise) – Rufus L Aug 28 '18 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). – Rufus L Aug 28 '18 at 18:46
  • I edited my post. I don't want to store the script on a .ps file. – alyncibi Aug 28 '18 at 21:32

2 Answers2

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" 
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?

alyncibi
  • 13
  • 4
  • 2
    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](http://meta.stackexchange.com/a/66378/248777). – mklement0 Aug 29 '18 at 00:01