1

I'm looking for a way from within a powershell script to run a second powershell script, where the parameters are stored in a string variable. Here is a - very simplified - example:

. $path\MainScript.ps1 -Mode $($objBMA.{Mode}) -InstallPath ${Env:ProgramFiles(x86)}

Like this everything works fine. But apparently you can't simply replace the parameter string "-Mode Install -InstallPath ${Env:ProgramFiles(x86)}" with a variable, like

$parameters = '-Mode Install -InstallPath ${Env:ProgramFiles(x86)}'
. $path\MainScript.ps1 $parameters

In this case, there's no variable expansion and parameter binding doesn't work as it should, meaning that the parameter "Mode" is not "Install" but "-Mode Install -InstallPath ${Env:ProgramFiles(x86)}".

Tested with the following script "MainScript.ps1":

param(
    [string]$Mode,
    [string]$InstallPath
)
"="*120
"Bound parameters passed to 'MainScript.ps1':"
$PSBoundParameters
"="*120
"Unbound parameters passed to 'MainScript.ps1':"
$args
"="*120

Config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <MainScriptName>MainScript.ps1</MainScriptName>
  <MainScriptParameters>-Mode $($objBMA.{Mode}) -InstallPath ${Env:ProgramFiles(x86)}</MainScriptParameters>

I tried "Invoke-Expression" (e.g. Invoke-Expression -Command 'Write-Output "$parameters"'), only to run into new problems - and also this command is said to be "dangerous"!? I suppose there must be a better solution!?

Background: I'm working on a helper script for software distribution, which automatically runs the main script with logging (Start-Transcription), error handling, and so on. The parameters for the main script are read from a config file.

3
  • What is the format of said "config file"? You should include it in your question. Commented Oct 5, 2020 at 12:42
  • I didn't think it's format was important, because I can change it's format any time. Anyway, it's just like this: <?xml version="1.0" encoding="utf-8" ?> <configuration> <MainScriptName>MainScript.ps1</MainScriptName> <MainScriptParameters>-Mode $($objBMA.{Mode}) -InstallPath ${Env:ProgramFiles(x86)}</MainScriptParameters> [...] Commented Oct 6, 2020 at 16:39
  • The format is important, bc now I see your file contains exactly what you'd normally type in the console. So you have no other choice than Invoke-Expression. Consider a more abstract format, e.g. the parameters could be separate nodes <params><param name="Mode"> etc., but if you want to use expressions inside the values, you still need to use Invoke-Expression. Commented Oct 7, 2020 at 8:19

1 Answer 1

0

You can use splatting here:

$parameters = @{
Mode='Install'
InstallPath=${Env:ProgramFiles(x86)}
}
ScriptFile @parameters

Unless you need iex here

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

3 Comments

Unfortunately this doesn't work, when the parameters are stored in a string variable, like in this case after reading them from a config file ...
@Marc worked for me in test...
Sorry, I don't understand. How did you convert the string from my question to the syntax needed for splatting?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.