Keywords: PowerShell | Remote Execution | Command Line Arguments | Invoke-Command | System Administration
Abstract: This technical paper provides an in-depth analysis of executing executable files with command line arguments on remote computers using PowerShell's Invoke-Command. It covers proper usage of the -ArgumentList parameter, handling executable paths with spaces, static parameter passing, and addresses common pitfalls. The paper also explores advanced topics including concurrent execution, permission management, and error handling strategies for system administrators.
Core Challenges in Remote Execution with Arguments
Executing executable files with command line arguments on remote computers is a common yet error-prone task in PowerShell remote management. Many administrators encounter issues where arguments fail to pass correctly, often due to misunderstandings about parameter passing mechanisms.
Correct Parameter Passing Methodology
Using the -ArgumentList parameter is crucial for solving remote argument passing issues. This method allows parameter values to be passed from the local session to the remote session's script block. The basic syntax is as follows:
Invoke-Command -ComputerName ComputerName -ScriptBlock {
param($ParameterName)
ExecutablePath $ParameterName
} -ArgumentList ParameterValue
Practical Implementation Examples
For simple command-line tools like ping, implementation can be achieved as follows:
Invoke-Command -ComputerName studio -ScriptBlock {
param($myarg)
ping.exe $myarg
} -ArgumentList localhost
When dealing with executable files containing spaces in their paths, the call operator & and quotes must be used:
Invoke-Command -ComputerName Computer1 -ScriptBlock {
param($myarg)
& 'C:\Program Files\program.exe' -something $myarg
} -ArgumentList "myArgValue"
Handling Static Parameters
If parameter values are static, they can be hardcoded directly in the script block, avoiding parameter passing:
Invoke-Command -ComputerName Computer1 -ScriptBlock {
& 'C:\Program Files\program.exe' -something "myArgValue"
}
Common Error Analysis
Many users attempt to pass the entire command as a string, which fails to properly parse arguments:
# Incorrect Example
$command = "program.exe -r param"
Invoke-Command -ComputerName $server -ScriptBlock {$command}
While this approach doesn't generate errors, arguments are not properly parsed, preventing the command from executing as expected.
Advanced Application Scenarios
In actual deployment scenarios, file copying and installation operations are typically combined. The reference article case demonstrates a complete deployment workflow:
$computername = Get-Content servers.txt
$sourcefile = "c:\shared\install.exe"
foreach ($computer in $computername) {
$destinationFolder = "\\$computer\C$\Temp"
if (!(Test-Path -Path $destinationFolder)) {
New-Item $destinationFolder -Type Directory
}
Copy-Item -Path $sourcefile -Destination $destinationFolder
Invoke-Command -ComputerName $computer -ScriptBlock {
Start-Process "C:\Temp\Install.exe" -ArgumentList "/install", "/quiet", "/norestart" -Wait
}
}
Concurrent Execution Optimization
For tasks requiring extended runtime, jobs can be utilized to achieve concurrent execution:
$jobs = @()
$computername = Get-Content servers.txt
foreach ($computer in $computername) {
$job = Invoke-Command -ComputerName $computer -ScriptBlock {
& 'C:\Program Files\program.exe' -param "value"
} -AsJob
$jobs += $job
}
# Wait for all jobs to complete
$jobs | Wait-Job
$jobs | Receive-Job
Permission and Error Handling
Remote execution may encounter permission issues, requiring assurance of:
- Enabled PowerShell Remoting
- Appropriate credential usage
- Network connectivity issue handling
- Error handling and logging implementation
try {
Invoke-Command -ComputerName $computer -ScriptBlock {
param($arg)
& 'C:\Program Files\app.exe' $arg
} -ArgumentList "parameter" -ErrorAction Stop
} catch {
Write-Error "Remote execution failed: $($_.Exception.Message)"
}
Best Practices Summary
Successful remote argument execution requires: proper use of the -ArgumentList parameter, handling path space issues, adding appropriate wait mechanisms, implementing concurrent control, and robust error handling. The combination of these techniques can significantly enhance remote management efficiency and reliability.