powershell-Invoke命令:FilePath参数的值必须是Windows powershell脚本文件

powershell-Invoke命令:FilePath参数的值必须是Windows powershell脚本文件,powershell,Powershell,我有一个可重用的脚本,我一直在使用它成功地调用远程ps1文件,但现在我尝试调用远程批处理文件,我收到以下错误消息- 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

我有一个可重用的脚本,我一直在使用它成功地调用远程ps1文件,但现在我尝试调用远程批处理文件,我收到以下错误消息-

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

您应该使用
-ScriptBlock
参数而不是
-FilePath

Invoke命令-ComputerName-Servername-ScriptBlock{&“$using:ItemLocation$using:FileName”}-Authentication-CredSSP-Credential$Credential
或者,如果您使用的是PowerShell v2,它没有
$using:VariableName
语法:

Invoke命令-ComputerName-Servername-ScriptBlock{param($ItemLocation,$FileName)&“$ItemLocation$FileName”}-ArgumentList$ItemLocation,$FileName-Authentication-CredSSP-Credential$Credential

.bat
文件位于本地计算机上,是否要在远程计算机上调用它?@PetSerAl bat文件是远程的。请使用
-ScriptBlock
而不是
-FilePath
调用命令-ScriptBlock{&“$using:ItemLocation$using:FileName”}…
@PetSerAl提交作为解决方案,我会接受我实际上在谷歌上搜索了它并找到了这个-解决了它。正是您所说的:)只是好奇,但为什么您要在PowerShell中运行批处理文件,而PowerShell可能可以执行批处理文件的任何操作,并且做得更好?