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,427 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,441 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. Andreas Baumgarten 99,376 Reputation points MVP
    2021-02-21T19:15:00.147+00:00

    Hi @corne nietnodig ,

    The path of the Copy-Item looks wrong: \\share\uninstalsp.bat
    It should be `\computer\share\uninstalsp.bat``

    The destination of Copy-Item is an administrative share (\\$computername\c$).
    The user running the script needs to be local administrator to access \\$computername\c$.

    On the remote computer the files are in C:\temp?

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. corne nietnodig 196 Reputation points
    2021-02-22T09:21:12.347+00:00

    Thanks andreas and MotoX, it looks like this is working

    0 comments No comments

  3. corne nietnodig 196 Reputation points
    2021-02-22T14:45:09.02+00:00

    I would like to expand the script after this software is uninstalled in the same script install new software so i have added the line:

    Get-Package -Name antivirus-oud | Uninstall-Package
    Start-Process msiexec.exe -Wait "/i \$computername\c$\temp\antivirus.msi /qn"

    First one is to make sure antivirus-oud is uninstalled (although it is Sophos and there is always something left behind in programms) and the second one is installing a new virusscan. These commands i have put right beneath the copy-item commands and started a new copy-item at the begginning to copy the new antivirus msi to the local machine.

    How ever it looks like the new antivirus install is popping up on my own computer when leaving /qn away. Must the line start process be elsewhere in the script?

    0 comments No comments

  4. MotoX80 32,441 Reputation points
    2021-02-22T15:48:38.327+00:00

    These commands i have put right beneath the copy-item

    Those statement need to be included in the script block so that they are invoked on the target pc.

    You should also be adding error checking along the way to verify that a given step was successful before executing the next step.

    Normally with an AV uninstall/reinstall, a reboot or 2 is required along the way.

    $sb = {
         "Calling uninstall"
         cmd.exe /c "C:\temp\uninstallsp.bat" 2>&1
         ""
         "Calling bas"
         cmd.exe /c "C:\temp\bas.bat"  2>&1
         "Uninstall old software."
          Get-Package -Name antivirus-oud | Uninstall-Package
          "Install new software"
           Start-Process msiexec.exe -Wait "/i c:\temp\antivirus.msi /qn"
     }
     $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