title: Invoke a second script with arguments from a script
body: <p>I have a script that reads a configuration file that results in a set of name value pairs that I would like to pass as arguments to a function in a second PowerShell script.</p>
  <p>I do not know what parameters will be placed in this configuration file at design time, so right at the point where I need to invoke this second PowerShell script, I basically just have one variable that has the path to this second script, and a second variable that is an array of arguments to pass to the script identified in the path variable.</p>
  <p>So the variable containing the path to the second script (<code>$scriptPath</code>), might have a value like:</p>
  <pre><code>&quot;c:\the\path\to\the\second\script.ps1&quot;
  </code></pre>
  <p>The variable containing the arguments (<code>$argumentList</code>) might look something like:</p>
  <pre><code>-ConfigFilename &quot;doohickey.txt&quot; -RootDirectory &quot;c:\some\kind\of\path&quot; -Max 11
  </code></pre>
  <p>How do I get from this state of affairs to the execution of script.ps1 with all of the arguments from $argumentList?</p>
  <p>I'd like any write-host commands from this second script to be visible to the console from which this first script is invoked.</p>
  <p>I have tried dot-sourcing, Invoke-Command, Invoke-Expression, and Start-Job, but I haven't found an approach that doesn't produce errors.</p>
  <p>For example, I thought the easiest first route was to try Start-Job called as follows:</p>
  <pre><code>Start-Job -FilePath $scriptPath -ArgumentList $argumentList
  </code></pre>
  <p>...but this fails with this error:</p>
  <pre><code>System.Management.Automation.ValidationMetadataException:
  Attribute cannot be added because it would cause the variable
  ConfigFilename with value -ConfigFilename to become invalid.
  </code></pre>
  <p>...in this case, &quot;ConfigFilename&quot; is the first parameter in the param list defined by the second script, and my invocation is apparently trying to set its value to &quot;-ConfigFilename&quot;, which is obviously intended to identify the parameter by name, not set its value.</p>
  <p>What am I missing?</p>
  <p><strong>EDIT:</strong></p>
  <p>Ok, here is a mock-up of the to-be-called script, in a file named invokee.ps1</p>
  <pre><code>Param(
  [parameter(Mandatory=$true)]
  [alias(&quot;rc&quot;)]
  [string]
  [ValidateScript( {Test-Path $_ -PathType Leaf} )]
  $ConfigurationFilename,
  [alias(&quot;e&quot;)]
  [switch]
  $Evaluate,
  [array]
  [Parameter(ValueFromRemainingArguments=$true)]
  $remaining)
  
  function sayHelloWorld()
  {
      Write-Host &quot;Hello, everybody, the config file is &lt;$ConfigurationFilename&gt;.&quot;
      if ($ExitOnErrors)
      {
          Write-Host &quot;I should mention that I was told to evaluate things.&quot;
      }
      Write-Host &quot;I currently live here: $gScriptDirectory&quot;
      Write-Host &quot;My remaining arguments are: $remaining&quot;
      Set-Content .\hello.world.txt &quot;It worked&quot;
  }
  
  $gScriptPath = $MyInvocation.MyCommand.Path
  $gScriptDirectory = (Split-Path $gScriptPath -Parent)
  sayHelloWorld
  </code></pre>
  <p>...and here is a mock-up of the calling script, in a file named invoker.ps1:</p>
  <pre><code>function pokeTheInvokee()
  {
      $scriptPath = (Join-Path -Path &quot;.&quot; -ChildPath &quot;invokee.ps1&quot;)
      $scriptPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($scriptPath)
      
      $configPath = (Join-Path -Path &quot;.&quot; -ChildPath &quot;invoker.ps1&quot;)
      $configPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($configPath)
      
      $argumentList = @()
      $argumentList += (&quot;-ConfigurationFilename&quot;, &quot;`&quot;$configPath`&quot;&quot;)
      $argumentList += , &quot;-Evaluate&quot;
  
      Write-Host &quot;Attempting to invoke-expression with: `&quot;$scriptPath`&quot; $argumentList&quot;
      Invoke-Expression &quot;`&quot;$scriptPath`&quot; $argumentList&quot;
      Invoke-Expression &quot;.\invokee.ps1 -ConfigurationFilename `&quot;.\invoker.ps1`&quot; -Evaluate
      Write-Host &quot;Invokee invoked.&quot;
  }
  
  pokeTheInvokee
  </code></pre>
  <p>When I run invoker.ps1, this is the error I'm currently getting on the first call to Invoke-Expression:</p>
  <pre><code>Invoke-Expression : You must provide a value expression on
  the right-hand side of the '-' operator.
  </code></pre>
  <p>The second call works just fine, but one significant difference is that the first version is using arguments whose paths have spaces in them, and the second does not.  Am I mishandling the presence of spaces in these paths?</p>