Run Powershell script files

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

Note: Powershell scripts must be available in the endpoint.

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

powershell script listlogfiles.ps1

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

sample output would be as below,

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

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

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