locked
How do I use powershell to run a batch file as a remote computer/server RRS feed

  • Question

  • I have a batch file which works fine on my local machine but i am trying to run it on a remote server. it keeps giving me errors does anybody have an idea on what i am missing out on. Below is the code and the error i get. 

    Invoke-Command –ComputerName "HEPWIN2012-03" -ScriptBlock {Start-Process "C:\DHCP\batfile1.bat"}

    the error i get is below 

    This command cannot be run due to the error: The system cannot find the file specified.
        + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
        + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessComm 
       and
        + PSComputerName        : HEPWIN2012-03

    Monday, December 10, 2018 3:07 PM

Answers

  • Hi,

    Thanks for your question.

    This is the double hop problem. The credential used to authenticate with HEPWIN2012-03 cannot be used by HEPWIN2012-03 to authenticate to the fileserver. Or any other network resources for that matter. That second hop is anything that requires authentication that is not on the first remote system.

    We can either pre-copy the file or re-authenticate on the remote end.

    For example:

    $computerName = 'HEPWIN2012-03'
    Copy-Item -path C:\DHCP\batfile1.bat -Destination \\$computername\c$\windows\temp\batfile1.bat
    invoke-command -computername $computerName -scriptblock{Start-Process "C:\windows\temp\batfile1.bat"}

    Best Regards,

    Lee


    Just do it.

    Tuesday, December 11, 2018 5:50 AM

All replies

  • Please change the path "C:\DHCP\batfile1.bat" to UNC path like \\servname\c$\DHCP\batfile1.bat and test the results.

    Monday, December 10, 2018 3:14 PM
  • nop that does not work same error
    • Edited by pyrrhustheo Monday, December 10, 2018 3:27 PM
    Monday, December 10, 2018 3:24 PM
  • The path to the file is correct? Whether UNC or locally on the server you are running Invoke-Command on the file exists in that location?
    Monday, December 10, 2018 3:30 PM
  • The file must exist on the remote machine.  You cannot specify  batch file on the local system to be run remotely.


    \_(ツ)_/

    Monday, December 10, 2018 7:09 PM
  • Hi,

    Thanks for your question.

    This is the double hop problem. The credential used to authenticate with HEPWIN2012-03 cannot be used by HEPWIN2012-03 to authenticate to the fileserver. Or any other network resources for that matter. That second hop is anything that requires authentication that is not on the first remote system.

    We can either pre-copy the file or re-authenticate on the remote end.

    For example:

    $computerName = 'HEPWIN2012-03'
    Copy-Item -path C:\DHCP\batfile1.bat -Destination \\$computername\c$\windows\temp\batfile1.bat
    invoke-command -computername $computerName -scriptblock{Start-Process "C:\windows\temp\batfile1.bat"}

    Best Regards,

    Lee


    Just do it.

    Tuesday, December 11, 2018 5:50 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 24, 2018 5:45 AM
  • Try 

    Start-Process -FilePath "C:\DHCP\batfile1.bat"


    Sunday, January 19, 2020 10:04 AM