将外部程序的参数作为字符串变量传递
Passing parameters for external program as string variable
我有一个简单的命令,需要通过远程计算机上的 PowerShell 执行。
E:\Programs\GMM\bin\GMMFailoverTool.exe -mssql="Server=SomeServer;Database=GMM01" list
我遇到的问题是使用 PowerShell 正确执行它,即使没有尝试通过 Invoke-Command
执行此操作。
$binary = "E:\Programs\GMM\bin\GMMFailoverTool.exe"
$command = "-mssql=`"Server=SomeServer;Database=gmm01`" list"
Write-Host BINARY: $binary -ForegroundColor Yellow
write-Host ARGS: $command -ForegroundColor Yellow
Write-Host FullCommand: $binary $command -ForegroundColor Yellow
& $binary $command
输出:
BINARY: E:\Programs\GMM\bin\GMMFailoverTool.exe
ARGS: -mssql="Server=SomeServer;Database=gmm01" list
FullCommand: E:\Programs\GMM\bin\GMMFailoverTool.exe -mssql="Server=SomeServer;Database=gmm01" list
而命令的 return 就像它根本没有获得任何参数(或者那些不正确)。
问题是如何正确传递那些已经定义了 $command
的参数?如果我手动完成而不将其全部放入变量中,它就可以工作......
& "E:\Programs\GMM\bin\GMMFailoverTool.exe" -mssql="Server=SomeServer;Database=gmm01" list
将参数作为数组传递:
$command = '-mssql="Server=SomeServer;Database=gmm01"', 'list'
& $binary $command
此外,在某些情况下,将参数正确传递给外部程序的唯一方法是 运行 使用 cmd.exe
:
的命令
$command = '-mssql="Server=SomeServer;Database=gmm01" list'
cmd /c "$binary $command"
为了 运行 远程命令,您需要在脚本块中定义变量:
Invoke-Command -Computer 'remotehost.example.com' -ScriptBlock {
$binary = ...
$command = ...
& $binary $command
}
或者(如果 $command
的值是由其他函数生成的,可能更好)通过参数 -ArgumentList
:
将它们传递到脚本块中
$binary = ...
$command = ...
Invoke-Command -Computer 'remotehost.example.com' -ScriptBlock {
& $args[0] $args[1]
} -ArgumentList $binary, $command
因为脚本块的内容对脚本的其余部分一无所知。
我有一个简单的命令,需要通过远程计算机上的 PowerShell 执行。
E:\Programs\GMM\bin\GMMFailoverTool.exe -mssql="Server=SomeServer;Database=GMM01" list
我遇到的问题是使用 PowerShell 正确执行它,即使没有尝试通过 Invoke-Command
执行此操作。
$binary = "E:\Programs\GMM\bin\GMMFailoverTool.exe"
$command = "-mssql=`"Server=SomeServer;Database=gmm01`" list"
Write-Host BINARY: $binary -ForegroundColor Yellow
write-Host ARGS: $command -ForegroundColor Yellow
Write-Host FullCommand: $binary $command -ForegroundColor Yellow
& $binary $command
输出:
BINARY: E:\Programs\GMM\bin\GMMFailoverTool.exe
ARGS: -mssql="Server=SomeServer;Database=gmm01" list
FullCommand: E:\Programs\GMM\bin\GMMFailoverTool.exe -mssql="Server=SomeServer;Database=gmm01" list
而命令的 return 就像它根本没有获得任何参数(或者那些不正确)。
问题是如何正确传递那些已经定义了 $command
的参数?如果我手动完成而不将其全部放入变量中,它就可以工作......
& "E:\Programs\GMM\bin\GMMFailoverTool.exe" -mssql="Server=SomeServer;Database=gmm01" list
将参数作为数组传递:
$command = '-mssql="Server=SomeServer;Database=gmm01"', 'list'
& $binary $command
此外,在某些情况下,将参数正确传递给外部程序的唯一方法是 运行 使用 cmd.exe
:
$command = '-mssql="Server=SomeServer;Database=gmm01" list'
cmd /c "$binary $command"
为了 运行 远程命令,您需要在脚本块中定义变量:
Invoke-Command -Computer 'remotehost.example.com' -ScriptBlock {
$binary = ...
$command = ...
& $binary $command
}
或者(如果 $command
的值是由其他函数生成的,可能更好)通过参数 -ArgumentList
:
$binary = ...
$command = ...
Invoke-Command -Computer 'remotehost.example.com' -ScriptBlock {
& $args[0] $args[1]
} -ArgumentList $binary, $command
因为脚本块的内容对脚本的其余部分一无所知。