windowspowershellpsexec

How do I run a Powershell script remotely as admin? (PsExec)


I would like to use PsExec to run a Powershell script (on a server) remotely on a computer as an administrator.

Unfortunately this does not work: psexec -s -i \computername Powershell \server\CNC_install.ps1 -Force

It keeps returning me "Access denied".


Solution

  • Normally you would do this using PowerShell remoting and not psexec using

    invoke-command

    See help invoke-command -examples for some examples.
    invoke-command executes a script block, so you would need to enter the content of your script in that part.

    On Windows clients, you would need to enable remoting first.

    But then there's the issue of double hopping. As your password can't normally be reused in a remote session. The remote host can't use your credentials in turn to contact other computers on your behalf.