2015-05-20 31 views
1

我有一個重複使用的腳本,我一直在成功調用遠程ps1文件,但現在我試圖調用遠程批處理文件,我得到了以下錯誤消息 -powershell - Invoke-Command:FilePath參數的值必須是Windows PowerShell腳本文件

Invoke-Command : The value of the FilePath parameter must be a Windows 
PowerShell script file. Enter the path to a file with a .ps1 file name 
extension and try the command again. 

這是腳本 -

#Admin Account 
$AdminUser = "domain\svc_account" 
$Password = Get-Content D:\scripts\pass\crd-appacct.txt | convertto-securestring 
$Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $AdminUser, $Password 
$FileName = "runme.bat" 
$ItemLocation = "D:\path\to\bat\" 

#Invoke Script Remotely 
Invoke-Command -ComputerName Servername -filepath "$ItemLocation$FileName" -Authentication CredSSP -Credential $Credential 
+0

'位於本地計算機上.bat'文件,你要調用它的遠程計算機上? – PetSerAl

+0

@PetSerAl bat文件是遠程的。 – whoisearth

+1

使用'-ScriptBlock'而不是'-FilePath':'Invoke-Command -ScriptBlock {&「$ using:ItemLocation $ using:FileName」} ...'。 – PetSerAl

回答

3

您應該使用-ScriptBlock參數,而不是-FilePath

Invoke-Command -ComputerName Servername -ScriptBlock {& "$using:ItemLocation$using:FileName"} -Authentication CredSSP -Credential $Credential 

或者,如果你正在使用的PowerShell v2的,不具有$using:VariableName語法:

Invoke-Command -ComputerName Servername -ScriptBlock {param($ItemLocation,$FileName) & "$ItemLocation$FileName"} -ArgumentList $ItemLocation,$FileName -Authentication CredSSP -Credential $Credential