Running Batch Script on remote Server via PowerShell

Stephen Sugumar picture Stephen Sugumar · Aug 20, 2015 · Viewed 60.3k times · Source

I need to connect to some remote servers from a client (same domain as the servers) once connected, I need to run a batch file:

I've done so with this code:

$Username = 'USER'
$Password = 'PASSWORD'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass

try {
    Invoke-Command -ComputerName "SERVER1" -Credential $Cred -ScriptBlock -ErrorAction Stop {
        Start-Process "C:\Users\nithi.sundar\Desktop\Test.bat"
    }
} catch {
    Write-Host "error"
}

This script does not give any errors, but it doesn't seem to be executing the batch script.

any input on this would be greatly appreciated.

Answer

Nick Eagle picture Nick Eagle · Aug 20, 2015

Try replacing

invoke-command -computername "SERVER1" -credential $Cred -ScriptBlock -ErrorAction stop { Start-Process "C:\Users\nithi.sundar\Desktop\Test.bat" }

with

Invoke-Command -ComputerName "Server1" -credential $cred -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:"cmd.exe /c 'C:\Users\nithi.sund
ar\Desktop\Test.bat'"}