0

I simply want to be able to pass one parameter from one PS script to another, currently my script (script1) is as follows (all thanks to user CB):

$filepath = Resolve-Path "script2.ps1"
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`"" 

This succesfully opens another script in via another powershell instance. Now i want to be able to carry across a parameter to the 'script2.ps1' script. I have tried the following but this doesnt not work:

script1.ps1

$name = read-host "The name" 

$filepath = Resolve-Path "script2.ps1"
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`"-name $name" 

script2.ps1

Param(

  [string]$name

)

write-host $name

This should simply pass over $name from script1 into $name in script2. I think im close but not quite close enough!

Thanks for any help!

2 Answers 2

0

The only problem I see is that you are missing a space after the last escaped ", try this:

 start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`" -name $name"
Sign up to request clarification or add additional context in comments.

2 Comments

Strikes again! Thanks very much for your help its much appreciated
@user3402227 Glad to help! As per the asnwer of Duncan... I'm curious me too to know why you use start-process in this case ;)
0

Is there a specific reason why you want to run the second script in a separate instance of Powershell? If not, you would do much better just to run the script directly:

$name = read-host "The name" 
.\script2.ps1 -name $name

This way you don't have to worry about escaping any of the parameters.

The way you were doing it forces all of the parameters to be converted to strings and processed by Windows command line processing. That can be a nightmare to ensure values get through in a usable form. If instead you just invoke the script directly you can pass objects as parameters and Powershell is all about using objects rather than strings.

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.