2015-05-19 87 views
1

我試圖通過PowerShell腳本使用invoke-command使用配置文件作爲參數運行可執行文件。使用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 
+1

那麼,你面臨的實際問題是什麼?你描述了你正在嘗試做什麼(這應該是AFAICS的工作原理),但沒有發生什麼問題。 –

+0

@AnsgarWiechers我編輯了該問題以包含錯誤。 – ASindleMouat

+0

我會建議檢查這個問題。 http://stackoverflow.com/questions/4225748/how-do-i-pass-named-parameters-with-invoke-command – Ironic

回答

1

代碼看起來應該工作。但是異常消息顯然缺少&符號。我會先檢查一下,因爲你收到的消息與&丟失時的預期完全相同。所以也許有保存的文件有問題,而不是你的代碼。

邊注:如果您在PowerShell的3.0或更新版本,你應該考慮使用$using:範圍在腳本塊,以避免增加了param-ArgumentList

$config = 'D:\EmailLoader\App.config' 
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock { 
    &'D:\EmailLoader\GetMailAndAttachment.exe' $using:config 
} 
+0

我想我已經混淆了,頂部的腳本似乎運行正常,我運行的腳本返回錯誤消息沒有在ScriptBlock中的字符串前的&。太多的腳本變體打開! – ASindleMouat

0

沒有必要將$config作爲參數傳遞給ScriptBlock。也不需要兩次添加$config參數。這應該工作:

$config = 'D:\EmailLoader\App.config' 
Invoke-Command -ComputerName SERVER-NAME -ScriptBlock {&('D:\EmailLoader\GetMailAndAttachment.exe')} -ArgumentList @($config) 
+0

返回以下錯誤:+ CategoryInfo:NotSpecified:(:字符串)[],的RemoteException + FullyQualifiedErrorId:NativeCommandError 未處理的異常: System.NullReferenceException:未設置爲一個對象的實例對象引用。 at GetMailAndAttachment.GMAA.ReadConfig(String ConfigFile,String Key) at GetMailAndAttachment.GMAA.Main(String [] args) – ASindleMouat

+0

我做了一個更改,以確保參數列表是een數組對象。如果這不起作用,那麼問題是在EmailLoader程序 – Rubanov

+0

它似乎仍然拋出相同的錯誤 – ASindleMouat

相關問題