Announcement

Collapse
No announcement yet.

Run Powershell script files

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Run Powershell script files

    Please use below sample script to execute powershell scripts via python,

    Note: Powershell scripts must be available in the endpoint.


    Code:
    import os;
    setpolicy=os.popen('powershell "Set-ExecutionPolicy -ExecutionPolicy Unrestricted" ').read();
    print(setpolicy);
    run=os.popen('powershell "C:\\Users\\Max\\Desktop\\listlogfiles.ps1" ').read();
    print(run)


    powershell script listlogfiles.ps1

    Code:
    write-host "List of log files in the mentioned directory" ;
    Get-Childitem C:\Windows\*.log


    sample output would be as below,
    Click image for larger version

Name:	execute_powershell.jpg
Views:	1
Size:	49.4 KB
ID:	4823





    Last edited by mkannan; 09-22-2016, 10:39 AM.

  • #2
    Just as a note, I would set the policy to RemoteSigned rather than Unrestricted or change the following line

    From:
    run=os.popen('powershell "C:\\Users\\Max\\Desktop\\listlogfiles.ps1" ').read();

    To:
    run=os.popen('powershell -ExecutionPolicy Bypass "C:\\Users\\Max\\Desktop\\listlogfiles.ps1" ').read();

    Then it will run and you don't have to change the default security policy on powershell
    Last edited by dougaust; 09-22-2016, 10:53 PM. Reason: Fixed formatting

    Comment

    Working...
    X