How can I run a .exe as an on a remote computer/computers with elevated privileges using a poweshell script?

16 views Asked by At

I need to run a Powershell script that executes a specific .exe file at a specific windows file path. The .exe file needs to be run with certain custom parameters and it needs to be run as an administrator. The file path to the EXE is the same on every machine.

It's easy enough to do this using a batch file while sitting at the computer, but I need to run this on every machine in the organization.

My thought was to use Enter-PSSession at the very least so that my team can remote it to each computer and run the commands.

Below is the script I've been trying to get to work.

I am able to enter-pssession with a remote computer, and it appears the EXE is called, but the expected result does not succeed. I believe that it is not being run as an Administrator.

How do I fix this?

#Here is the script I have been trying to get to work:



# Define the remote computer name and credentials
$remoteComputer = "computername"
$credential = Get-Credential

# Define the path to the executable and the specific switches
$executablePath = "C:\Program Files (x86)\path\to\file.exe"
$switches = "-address url.domain.org"

# Enter an elevated PowerShell session on the remote computer
Enter-PSSession -ComputerName $remoteComputer -RunAsAdministrator True -Credential $credential

# Run the executable with the specified switches
Invoke-Command -ScriptBlock {
    param (
        $executablePath,
        $switches
    )
    Start-Process -FilePath $executablePath -ArgumentList $switches -Wait
} -ArgumentList $executablePath, $switches

# Exit the remote PowerShell session
Exit-PSSession
0

There are 0 answers