2012-06-26 31 views
1

我試圖在PowerShell腳本中遠程執行.bat文件。在我的腳本文件中的代碼看起來是這樣的:在PS會話期間執行.cmd文件時出錯

$server 
$cred 
# Both of these methods are strings pointing to the location of the scripts on the 
# remote server ie. C:\Scripts\remoteMethod.cmd 
$method1 
$method2 
$scriptArg 

Enter-PSSession $server -Credential $cred 
Invoke-Command {&$method1} 
if($?) 
{ 
    Invoke-Command {&$method2 $args} -ArgumentList $scriptArg 
} 
Exit-PSSession 

但每當我運行它,我得到

術語「C:\腳本\ remoteMethod.cmd」沒有被識別爲名稱一個cmdlet,函數,腳本文件或可操作的程序。檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然後重試。

出現這種情況,即使我身邊,我發現這個問題Using Powershell's Invoke-Command to call a batch file with arguments並試圖接受的答案無濟於事方法被調用的順序。

切換。

回答

1

我不希望$ method1和$ method2變量在腳本塊中可用於Invoke-Command。通常你會通過-ArgumentList參數例如爲:

Invoke-Command {param($method, $arg) &$method $arg} -Arg $method2 $scriptArg 

通過那些不過好像你的情況的路徑CMD文件通過遠程連接使得它。試試看看問題是否與遠程機器上的ComSpec環境變量相關:

Invoke-Command {param($method, $arg) cmd.exe /c $method $arg} -Arg $method2, $scriptArg 
+0

謝謝,這似乎已經做到了。現在它工作正常 – winthethird