Python Forum
Triggering a ps1 script in remote windows server via http python request - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Triggering a ps1 script in remote windows server via http python request (/thread-41493.html)



Triggering a ps1 script in remote windows server via http python request - jasveerjassi - Jan-25-2024

I am trying to trigger a ps1 script in remote windows 2016 web server via http get request. This is the code i am trying.

Tried with GET and POST request and receive http 200 status code each time. However powershell script is not getting triggered. Windows server settings is getting checked alongside however wanted to check here if this code looks fine and workavble solution

def trigger_remote_powershell(remote_url, script_path):
    # Prepare the PowerShell command to execute on the remote machine
    powershell_command = f"Invoke-Expression (New-Object Net.WebClient).DownloadString('{remote_url}/{script_path}');"

    # Send the HTTP POST request
    payload = {'command': powershell_command}
    response = requests.post('http://your_remote_machine_url', data=payload)

    # Check the response
    if response.status_code == 200:
        print('PowerShell script triggered successfully.')
    else:
        print(f'Error triggering PowerShell script. Status code: {response.status_code}')

if __name__ == "__main__":
    # Replace the placeholders with your actual values
    remote_url = 'http://your_remote_machine_url'
    script_path = 'path/to/your/script.ps1'

    trigger_remote_powershell(remote_url, script_path)



RE: Triggering a ps1 script in remote windows server via http python request - deanhystad - Jan-26-2024

What does your_remote_machine_url do when it gets a POST request? Does it know the post request contains a powershell command it is supposed to execute?