obi
obi

Reputation: 1

How do I use Invoke-Command to run a bath file as a remote server?

I just wrote a batch file that I would like run as a remote server but it keeps giving me errors in PowerShell when I use Invoke-Command. The batch file runs fine. Does anybody have idea what am missing out on? Below is my PowerShell script and the error I get.

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

The error I get is:

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

Upvotes: 0

Views: 166

Answers (1)

HariHaran
HariHaran

Reputation: 4179

FYI , If the remote computer has credentials you should pass that too. And instead of Start-Process use the Invoke-Expression. The below should work.

$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'"}

Upvotes: -1

Related Questions