Run PowerShell script on remote server as different user without any pre-requisites

Viewed 33

I need to trigger the execution of a powershell script on remote server under different user. This script uses 'gpg.exe' so the execution has to be on the remote server with a path pointing to 'gpg.exe'. I created a bat file on remote server with following commands:

REM RUNTEST.BAT
time /T
SET PowerShellDir=C:\Windows\System32\WindowsPowerShell\v1.0
CD /D "%PowerShellDir%"
echo started
start /wait powershell.exe "Start-Transcript -Path D:\logs\testlog.txt;import-module 
'\\ServerName\FolderName\Script\AutomationScript.ps1';Stop-Transcript"
time /T
echo done
exit /b

Then invoked this bat file from PowerShell on another machine:

runas /user:DOMAIN\serviceuser "\\ServerName\FolderName\Script\Execute.bat"

These steps do execute the PowerShell script successfully and the output is also as expected. However, this requires 'gpg.exe' to be available on the path mentioned in the PowerShell script on the machine which will invoke the execution. Is there a way the script can look for pre-requisites (like gpg.exe, folder structure etc) only on the remote server, not the machine which invoked the bat file?

Not sure if OS makes any difference, the remote server has Windows Server 2019 and calling machine has Win 10. The remote server has the required folder structure, gpg.exe, bat file to execute the PowerShell script successfully. The calling machine will not have 'gpg.exe' & the folder structure.

0 Answers
Related