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.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Marc
  • 131
  • 1
  • 5
  • What is the format of said "config file"? You should include it in your question. – marsze Oct 05 '20 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: MainScript.ps1 -Mode $($objBMA.{Mode}) -InstallPath ${Env:ProgramFiles(x86)} [...] – Marc Oct 06 '20 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 `` etc., but if you want to use expressions inside the values, you still need to use Invoke-Expression. – marsze Oct 07 '20 at 08:19

1 Answers1

0

You can use splatting here:

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

Unless you need iex here

Wasif
  • 14,755
  • 3
  • 14
  • 34
  • 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 Oct 05 '20 at 10:10
  • @Marc worked for me in test... – Wasif Oct 05 '20 at 15:49
  • Sorry, I don't understand. How did you convert the string from my question to the syntax needed for splatting? – Marc Oct 06 '20 at 16:43