1

I have a simple Powershell script which accepts 2 parameters (script1.ps1):

[CmdletBinding()]
param (
    [Parameter(Mandatory=$true)][object] $parameter1,
    [Parameter(Mandatory=$true)][object] $parameter2
)

Write-Output "executing script...."

From a different script, I load the parameters and call the script above:

$parameters = @{
    parameter1 = $parameter1
    parameter2 = $parameter2
}

# execute script with parameters
& "./script1.ps1" $parameters

When I run this script which calls script1.ps1 in Debug mode, I always get prompted for parameter2, even though parameter2 contains a value. If I then supply a value at the prompt and then hit the breakpoint in script1.ps1, I can hover over $parameter1, and it contains a collection of both parameters.

How do I do this correctly so that the parameters show up in script1.ps1 in the correct position?

Mike
  • 1,010
  • 1
  • 14
  • 33
  • 4
    To pass a set of parameter through a hashtable, you need to do splatting. Change the `$` for a `@` ... `& "./script1.ps1" @parameters` [About_Splatting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-7.2) – Sage Pourpre Jun 09 '22 at 03:25
  • 2
    Change `$parameters` for `@parameters` thats how splatting works – Santiago Squarzon Jun 09 '22 at 03:25
  • You could use a directory to store the variables. It would make it easier to call them in either script. – Vilestorm Jun 09 '22 at 10:39
  • @SantiagoSquarzon thanks very much for the quick response, now works as expected. – Mike Jun 09 '22 at 12:54
  • @SagePourpre thanks to you as well. I'd not heard of splatting prior and will definitely use more in the future. – Mike Jun 09 '22 at 12:55

0 Answers0