如何使用Invoke-Command将Bath文件作为远程服务器运行?

时间:2018-12-10 12:58:14

标签: powershell batch-file powershell-v2.0 powershell-v3.0

我刚刚编写了一个批处理文件,该文件想作为远程服务器运行,但是当我使用Invoke-Command时,它在PowerShell中不断给我带来错误。批处理文件运行正常。有人知道丢失了什么吗?以下是我的PowerShell脚本和出现的错误。

Invoke-Command -ComputerName "HEPWIN2020-10" -Scriptblock {Start-Process C:\DHCP\batfile1.bat}

我得到的错误是:

Invoke-Command : A positional parameter cannot be found that accepts argument ''.
At line:1 char:1
+ Invoke-Command -ComputerName "HEPWIN2012-03" -Scriptblock{Start-Proce ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.InvokeCommandCommand

1 个答案:

答案 0 :(得分:-1)

仅供参考,如果远程计算机具有凭据,那么您也应该通过该凭据。并且使用Start-Process代替Invoke-Expression。以下应该可以。

$Username = 'USER'
$Password = 'PASSWORD'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName "Server1" -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\DHCP\batfile1.bat'"}