Nick
Nick

Reputation: 189

Using "Invoke-Command" to run a local file on a remote machine

I am trying to use powershell to run a bat script that is located on Computer A and execute it on Computer B

Computer A is the computer calling the Invoke Command function

I am trying to do this via

Invoke-Command -ComputerName ComputerB -ScriptBlock{& "\\ComputerA\filepath\batch.bat"}  -Credential $Cred

When I run this I recieve the error

The term '\\ComputerA\filepath\batch.bat' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the  name, or if a path was included, verify that the path is correct and try again.

I have also tried to run it by calling cmd on Computer B and pass this bat script as an argument and I dont recieve any errors but I can tell the script did not run The command I use for this is

Invoke-Command -ComputerName ComputerB -ScriptBlock{& "C:\Windows\System32\cmd.exe"} -ArgumentList "\\ComputerA\filepath\batch.bat" -Credential $Cred

When I go onto Computer B and call the script manually through cmd it works

I confirmed the Invoke-Command works because I was able to run programs that were local to Computer B

I am trying to not have to put any files on computer B such as psexec or the bat script itself which is why I am trying it this way

Thank you

Upvotes: 2

Views: 8795

Answers (2)

Frode F.
Frode F.

Reputation: 54881

This sounds like the "double hop"-/"second hop"-problem. You're credentials aren't available in the remote session (to access the network share). Solutions:

  • Use CredSSP authentication
  • Run it locally..
    • Copy the file to the remote computer
    • Use Invoke-Command -Scriptblock { & "c:\temp\batch.bat" }
    • Remove fiel on the remote computer if necessary
  • Use psexec.exe

Upvotes: 2

Nana Lakshmanan
Nana Lakshmanan

Reputation: 739

When you remote to a machine (in this case ComputerB) and try to access something on ComputerA you are basically trying to access a network resource which by default isn't allowed. Use the -EnableNetworkAccess parameter in Invoke-Command to explicitly allow the same

Upvotes: 2

Related Questions