powershell run a batchfile or ps1 file on remote pc

corne nietnodig 196 Reputation points
2021-02-21T18:46:42.977+00:00

I have some trouble to start a batchfile or ps1 file on a remote system with invoke-command. I know that when starting such a script or batch in powershell on a networkshare that there is some problems with double hop. So i first copy the files to the local system, that is a challenge already because i the copy command is in the batchfile or script that i want to execute on the remote pc or server.

What i have tried is directly read the batch or ps1 from a networkshare but that gives me a access denied, i think double hop. So then next:
$computerName = 'testpc'
Copy-Item -path \share\uninstalsp.bat -Destination \$computername\c$\temp\uninstallsp.bat
Copy-Item -path \share\bas.bat -Destination \$computername\c$\temp\bas.bat
invoke-command -computername $computerName -scriptblock{Start-Process "C:\temp\uninstallsp.bat"}
invoke-command -computername $computerName -scriptblock{Start-Process "C:\temp\bas.bat"}

After this there is no error but nothing happens on the remote system. I have already had this before with other files, no error but nothing happens. In this example i try to uninstall a program, in the batchfile there are some registry keys changed and the uninstall certain software is executed so the last batchfile must wait for the first.

What am i doiing wrong? And how can i run this without copy the files to the local system and complicated double hop?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,376 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 31,656 Reputation points
    2021-02-21T22:01:55.07+00:00

    In addition to Andreas's comments, you are issuing a Start-Process but you are not waiting for it to finish. You would need to add -Wait to the command. But you would still be wondering what happened because you are not capturing stdout or stderr.

    Try this.

    $sb = {
        "Calling uninstall"
        cmd.exe /c "C:\temp\uninstallsp.bat" 2>&1
        ""
        "Calling bas"
        cmd.exe /c "C:\temp\bas.bat"  2>&1
    }
    $computerName = 'testpc'
    Copy-Item -path \\server\share\uninstalsp.bat -Destination \\$computername\c$\temp\uninstallsp.bat
    Copy-Item -path \\server\share\bas.bat -Destination \\$computername\c$\temp\bas.bat
    invoke-command -computername $computerName -scriptblock  $sb
    
    0 comments No comments

9 additional answers

Sort by: Most helpful
  1. corne nietnodig 196 Reputation points
    2021-02-25T07:28:16.653+00:00

    Thanks!, it looks like it is working now, the log file that is created is presented on the screen and installation is done on the remote machine..