How can I run a remote EXE with a variable path and arguments?

0
0
Asked By MellowPine42 On

I need to install an executable on remote computers with Invoke-Command. The command works when I put the full executable path and switches directly inside the script block, but some switch values change regularly, so I need to store the executable path and arguments in variables.

I tried passing a complete command string with -ArgumentList and referencing the same variable inside the script block, as well as using $using:. I also tried Start-Process, but neither approach worked. What is the correct way to pass a variable executable path and its arguments into Invoke-Command and wait for the installation to finish?

3 Answers

Answered By SilverCactus19 On

A single string such as 'Path.exe /switch1 /switch2' mixes the path and arguments together. For a native command, store them separately. Each argument can be its own array element, then splat the array:

$path = 'C:ToolsSetup.exe'
$args = @('/quiet','/key:12345')

Invoke-Command -ComputerName $computerName -ScriptBlock {
& $using:path @using:args
}

If the installer launches child processes, make sure the process you wait for actually represents the complete installation. Start-Process -Wait waits for the launched process and its children, while waiting on the process separately may only wait for the immediate executable.

Answered By QuietHarbor7 On

Use parameters in the remote script block to receive the values passed through -ArgumentList. Keep the executable path separate from its arguments, and invoke the executable with the call operator:

$exePath = 'C:InstallersPath.exe'
$exeArgs = @('/switch1','/switch2')

Invoke-Command -ComputerName $computerName -ArgumentList $exePath,$exeArgs -ScriptBlock {
param($ExePath,$ExeArgs)
& $ExePath @ExeArgs
}

You can also use Start-Process when appropriate:

Invoke-Command -ComputerName $computerName -ArgumentList $exePath,$exeArgs -ScriptBlock {
param($ExePath,$ExeArgs)
Start-Process -FilePath $ExePath -ArgumentList $ExeArgs -Wait
}

The -Wait is important for installers because otherwise the remote command may finish while the installer is still running, and the process can be terminated when the remoting session closes. With -ArgumentList, the script block must define parameters with param(); the original local variable names are not automatically available remotely. $using:exePath and $using:exeArgs are another option, but explicitly passing parameters is usually clearer.

Answered By BrightMap88 On

Invoke-Command runs the script block in a separate scope, so local variables need to be passed in or referenced with $using:. A parameter object is useful when there are several related values:

$install = [pscustomobject]@{
Path = 'C:ToolsSetup.exe'
Arguments = @('/quiet','/key:12345')
}

Invoke-Command -ComputerName $computerName -ArgumentList $install -ScriptBlock {
param($Install)
Start-Process -FilePath $Install.Path -ArgumentList $Install.Arguments -Wait -PassThru
}

The key points are defining param() in the script block, separating the file path from its arguments, and waiting for the installer when the remote session must remain active until installation completes.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.