运行 使用 Invoke-Command 的带有参数的可执行文件
Run an executable with a parameter using Invoke-Command
我正在尝试 运行 使用配置文件作为参数的可执行文件,通过 PowerShell 脚本使用 invoke-command。
这是我目前在 PowerShell 脚本中的内容:
$config = 'D:\EmailLoader\App.config'
invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config) & 'D:\EmailLoader\GetMailAndAttachment.exe' $config} -ArgumentList $config
然后我使用以下命令执行 PowerShell 脚本:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -File "D:\PowerShellScripts\Email.ps1"
我收到以下错误:
Unexpected token 'config' in expression or statement.
At D:\PowerShellScripts\Email.ps1:3 char:121
+ invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config) 'D:\EmailLoader\GetMailAndAttachment.exe' $config <<<< } -ArgumentList $config
+ CategoryInfo : ParserError: (config:String) [], ParentContainsE rrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
不需要将 $config
作为参数传递给 ScriptBlock
。也不需要两次添加 $config
参数。这应该有效:
$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {&('D:\EmailLoader\GetMailAndAttachment.exe')} -ArgumentList @($config)
代码看起来应该可以工作。但是异常消息显然缺少 &
符号。我会先检查一下,因为您收到的消息与 &
丢失时预计收到的消息完全相同。因此,保存的文件可能存在问题,而不是您的代码存在问题。
旁注:如果您使用的是 PowerShell 3.0 或更高版本,则应考虑在脚本块中使用 $using:
范围以避免添加 param
和 -ArgumentList
.
$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {
&'D:\EmailLoader\GetMailAndAttachment.exe' $using:config
}
我正在尝试 运行 使用配置文件作为参数的可执行文件,通过 PowerShell 脚本使用 invoke-command。
这是我目前在 PowerShell 脚本中的内容:
$config = 'D:\EmailLoader\App.config'
invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config) & 'D:\EmailLoader\GetMailAndAttachment.exe' $config} -ArgumentList $config
然后我使用以下命令执行 PowerShell 脚本:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy ByPass -File "D:\PowerShellScripts\Email.ps1"
我收到以下错误:
Unexpected token 'config' in expression or statement.
At D:\PowerShellScripts\Email.ps1:3 char:121
+ invoke-command -ComputerName SERVER-NAME -ScriptBlock {param($config) 'D:\EmailLoader\GetMailAndAttachment.exe' $config <<<< } -ArgumentList $config
+ CategoryInfo : ParserError: (config:String) [], ParentContainsE rrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
不需要将 $config
作为参数传递给 ScriptBlock
。也不需要两次添加 $config
参数。这应该有效:
$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {&('D:\EmailLoader\GetMailAndAttachment.exe')} -ArgumentList @($config)
代码看起来应该可以工作。但是异常消息显然缺少 &
符号。我会先检查一下,因为您收到的消息与 &
丢失时预计收到的消息完全相同。因此,保存的文件可能存在问题,而不是您的代码存在问题。
旁注:如果您使用的是 PowerShell 3.0 或更高版本,则应考虑在脚本块中使用 $using:
范围以避免添加 param
和 -ArgumentList
.
$config = 'D:\EmailLoader\App.config'
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {
&'D:\EmailLoader\GetMailAndAttachment.exe' $using:config
}