The Goal:
Is to be able to test to see if PowerShell v6 is installed (which is OK), and if it is, then to invoke that shell for certain CmdLets. This will be invoked within a script running in PowerShell v5.1. I cannot shift fully to v6 as there are other dependencies that do not yet work in this environment, however, v6 offers significant optimisations on certain CmdLets that lead to an improvement in operation of over 200 times (specifically, an Invoke-WebRequest
where the call will lead to a download of a large file - in v5.1 a 4GB file will take over 1 hour to download, in v6 this will take approximately 30 seconds using the same machines on the same subnet.
Additional Points:
However, I also build up a set of dynamic parameters that are used to splat into the CmdLets parameter list. For example, a built parameter list would look something like:
$SplatParms = @{
Method = "Get"
Uri = $resource
Credential = $Creds
Body = (ConvertTo-Json $data)
ContentType = "application/json"
}
And running the CmdLet normally would work as expected:
Invoke-RestMethod @SplatParms
What has been tried:
Over the past few days I have looked over various posts on this forum and elsewhere. We can create a simple script block the can be call and also works as expected:
$ConsoleCommand = { Invoke-RestMethod @SplatParms }
& $ConsoleCommand
However, attempting to pass the same thing in the Start-Process
CmdLet fails, as I guess the parameter hash table is not being evaluated:
Start-Process pwsh -ArgumentList "-NoExit","-Command &{$ConsoleCommand}" -wait
Results in:
Invoke-RestMethod : Cannot validate argument on parameter 'Uri'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At line:1 char:22
+ &{ Invoke-RestMethod @SplatParms }
Where next?
I guess I somehow have to pass in parameters as arguments so they can then be evaluated and splatted, however, the syntax eludes me. I'm not even sure if Start-Process
is the best CmdLet to use, but rather should I look to something else, like Invoke-Command
, or something completely different?
It would be awesome to get the result of this CmdLet back into the originating shell, but at the moment, it will simply take something that functions.