7

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.

2 Answers 2

11

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'"}
Sign up to request clarification or add additional context in comments.

2 Comments

I am trying to make the path dynamically, but it's giving error -> $toinstall = ('"' + "cmd.exe /c " + $inputdata.TargetFolder + '\' + $inputdata.InstallerBat + '"') ------------------------------- Invoke-Command -ComputerName $inputdata.TargetMachineName -ErrorAction Stop -ScriptBlock {Invoke-Expression -Command:$toinstall} ------------------------------- Error : Cannot bind argument to parameter 'Command' because it is null.
6

It's not possible that the code you posted ran without errors, because you messed up the order of the argument to Invoke-Command. This:

Invoke-Command ... -ScriptBlock -ErrorAction Stop { ... }

should actually look like this:

Invoke-Command ... -ErrorAction Stop -ScriptBlock { ... }

Also, DO NOT use Invoke-Expression for this. It's practically always the wrong tool for whatever you need to accomplish. You also don't need Start-Process since PowerShell can run batch scripts directly:

Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    C:\Users\nithi.sundar\Desktop\Test.bat
} -Credential $Cred -ErrorAction Stop

If the command is a string rather than a bare word you need to use the call operator, though:

Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    & "C:\Users\nithi.sundar\Desktop\Test.bat"
} -Credential $Cred -ErrorAction Stop

You could also invoke the batch file with cmd.exe:

Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    cmd /c "C:\Users\nithi.sundar\Desktop\Test.bat"
} -Credential $Cred -ErrorAction Stop

If for some reason you must use Start-Process you should add the parameters -NoNewWindow and -Wait.

Invoke-Command -ComputerName "SERVER1" -ScriptBlock {
    Start-Process 'C:\Users\nithi.sundar\Desktop\Test.bat' -NoNewWindow -Wait
} -Credential $Cred -ErrorAction Stop

By default Start-Process runs the invoked process asynchronously (i.e. the call returns immediately) and in a separate window. That is most likely the reason why your code didn't work as intended.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.