Question:
G’day everyone,
I’m trying to execute a function in PowerShell with the Parameters coming from a Variable I’m not sure if it’s possible in the way I want it to but maybe someone has any idea how I would go about doing that.
1 2 3 4 5 |
$scriptPath = "C:\temp\Create-File.ps1" $parameters = "-Path C:\temp\testfile.txt -DoSomethingSpecial" & $scriptPath $parameters |
Something along those lines, I don’t know in which order the Parameters get entered so I can’t use $args[n..m] or binding by position for that. Maybe there is some other Cmdlet I don’t know about that is capable of doing that?
Answer:
You can use a hastable and Splatting to do this.
Simply set each param name and value in the variable as you would a normal hastable, then pass this in using @params
syntax.
The switch param however, needs a $true
value for it to function correctly.
1 2 3 4 5 6 7 |
$params = @{ Path = 'C:\temp\testfile.txt' DoSomethingSpecial = $true } .\Create-File.ps1 @params |