Python Forum
Triggering a ps1 script in remote windows server via http python request
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Triggering a ps1 script in remote windows server via http python request
#1
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)
Larz60+ write Jan-25-2024, 11:14 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Code has been modified for you this time. Please use BBCode tags on future posts.
Reply
#2
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 555 Oct-03-2023, 11:15 AM
Last Post: snippsat
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,283 Jun-29-2023, 11:57 AM
Last Post: gologica
  Python script running under windows over nssm.exe JaroslavZ 0 725 May-12-2023, 09:22 AM
Last Post: JaroslavZ
  PsInfo from remote computer in python susja 3 2,283 Oct-10-2022, 08:39 AM
Last Post: DeaD_EyE
  How to modify python script to append data on file using sql server 2019? ahmedbarbary 1 1,232 Aug-03-2022, 06:03 AM
Last Post: Pedroski55
  How to compile a Python script for a Windows / Linux executable? netanelst 2 1,340 May-24-2022, 07:02 AM
Last Post: netanelst
  Setup Portable Python on Windows for script starts with double clicks? pstein 0 1,827 Feb-18-2022, 01:29 PM
Last Post: pstein
  Process the image on the Python HTTP server Aleks 0 3,218 Dec-02-2021, 11:43 PM
Last Post: Aleks
  Real-Time output of server script on a client script. throwaway34 2 2,062 Oct-03-2021, 09:37 AM
Last Post: ibreeden
  how can I correct the Bad Request error on my curl request tomtom 8 5,089 Oct-03-2021, 06:32 AM
Last Post: tomtom

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020