Running Powershell within Python Script

If you have a question or need help, this is the place to be.
Post Reply
stillirise
Posts: 4
Joined: Thu Nov 12, 2020 6:53 pm

Running Powershell within Python Script

Post by stillirise » Thu Nov 12, 2020 7:02 pm

Good day,

This is my first attempt at EventGhost, so please bear with me.

As part of an Unlock Workstation Macro, I have a Session.Unlock Event then a Python Script Action.

In the Python Script action, I have a Powershell script which turns on and off my Windows 10 bluetooth adapter based on time of day.

There is an error thrown, not in the EG log, but a command window pops up really fast then closes, so I can't read it.

2 questions:

1) Is this the right way to invoke my PS script?
2) How can I capture the error that is thrown?

I can successfully run the Powershell on the command line.

Thanks for any help.

Code: Select all

import datetime
import time
import subprocess
import sys
timestamp = datetime.datetime.now().time() # Throw away the date information
time.sleep(1)
start = datetime.time(7, 55)
end = datetime.time(12)
subprocess.Popen(["powershell.exe", "C:\\Users\\humble\\bluetooth.ps1 -BluetoothStatus Off"])

User avatar
Sem;colon
Plugin Developer
Posts: 753
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: Running Powershell within Python Script

Post by Sem;colon » Fri Nov 13, 2020 12:33 pm

Hi stillirise,

Welcome to EventGhost!

There is an action to run a command line command in EventGhost, you can simply configure it -> right click -> copy as python and paste it in your script.

I would also recommend to use the full path to PowerShell.exe, not just PowerShell.exe
If you like my work, Image me a drink :wink:

stillirise
Posts: 4
Joined: Thu Nov 12, 2020 6:53 pm

Re: Running Powershell within Python Script

Post by stillirise » Tue Nov 17, 2020 10:02 pm

Hi Sem;colon,

Thanks for the reply, but I'm not understanding what I should do.

When I Copy as Python from the Run Command action, I get this:

Code: Select all

eg.plugins.System.Command(u'd', True, False, u'', True, False, False, False, False)
What does that mean, and where should I paste it exactly?

I included the full path to PowerShell in my script now

Code: Select all

import datetime
import time
import subprocess
import sys
timestamp = datetime.datetime.now().time() # Throw away the date information
time.sleep(1)
start = datetime.time(7, 55)
end = datetime.time(12)
subprocess.Popen(["C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe", "C:\\Users\\humble\\bluetooth.ps1 -BluetoothStatus Off"])

User avatar
Sem;colon
Plugin Developer
Posts: 753
Joined: Sat Feb 18, 2012 10:51 am
Location: Germany

Re: Running Powershell within Python Script

Post by Sem;colon » Tue Nov 17, 2020 11:02 pm

Well, you need to paste it in your script instead of the subprocess.popen call.
But make sure you configure and test that action before you copy it!

But to be honest, I think the thing that actually is keeping your script from running is the PowerShell Execution policy.
Try to run this before you run your script (e.g. At EG start)

Code: Select all

Set-ExecutionPolicy Bypass -Scope Process
If you like my work, Image me a drink :wink:

stillirise
Posts: 4
Joined: Thu Nov 12, 2020 6:53 pm

Re: Running Powershell within Python Script

Post by stillirise » Wed Nov 18, 2020 11:57 pm

I can run the command from the Windows prompt just fine:

Code: Select all

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -command C:\Users\humble\bluetooth.ps1 -BluetoothStatus Off
I can't seem to make it work.

I can run from the command line successfully.

I can run a local python script successfully.

The python script action in EG is causing an error which I can't read because it pops up in a command window then closes but the message starts with "You cannot call a method on a null valued expression".

I tried both of your suggestions, Sem;colon, but no luck.

Thanks for the help anyways.

User avatar
kgschlosser
Site Admin
Posts: 5504
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Running Powershell within Python Script

Post by kgschlosser » Fri Nov 20, 2020 6:11 pm

Code: Select all

import datetime
import time
import subprocess
import sys
timestamp = datetime.datetime.now().time() # Throw away the date information
time.sleep(1)
start = datetime.time(7, 55)
end = datetime.time(12)
proc = subprocess.Popen('"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" "C:\\Users\\humble\\bluetooth.ps1" -BluetoothStatus Off"')
proc.communicate()
If you like the work I have been doing then feel free to Image

stillirise
Posts: 4
Joined: Thu Nov 12, 2020 6:53 pm

Re: Running Powershell within Python Script

Post by stillirise » Wed Nov 25, 2020 12:32 am

Hi,

Thanks for the reply but the same error is occurring:

Code: Select all

"You cannot call a method on a null valued expression".
I can run the same command on the python command line successfully - it turns off and on my bluetooth:

Code: Select all

>>> proc = subprocess.Popen('"C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe" "C:\\Users\\humble\\bluetooth.ps1" -BluetoothStatus Off')

Post Reply