I
I
Ilya2016-11-18 13:26:10
Python
Ilya, 2016-11-18 13:26:10

How to send commands from Python to PowerShell line by line?

Since I'm still not a chipper in Python and in programming, a detail has surfaced that I have to study. Based on my previous question ( https://toster.ru/q/371771), I got into PowerShell and was able to turn down the screen brightness with 2 simple commands.

$monitor=Get-WmiObject -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods
$monitor.WmiSetBrightness(0,50)

But this is in PowerShell. And I need to call PS through Python first, and then send the above commands. What am I doing.
import subprocess

proc = subprocess.Popen(['powershell', '$monitor=Get-WmiObject -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods', '$monitor.WmiSetBrightness(0,50)'])
proc.wait()

The codec does not work, it gives red letters in the console and writes something about the invoker.
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

The question is how to send these two elementary commands to elementary PowerShell, only in line order, and not in one line? For in the PSH shell familiar to these commands, the screen obediently adjusts the brightness, and through Python connects two commands along the way into one line.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-11-18
@hugga

As you have already written. Between PS commands you need ";" .
Replace:
on the:
The final code in this case:

import subprocess

proc = subprocess.Popen(['powershell', '$monitor=Get-WmiObject -Namespace root/WMI -ClassName WmiMonitorBrightnessMethods; $monitor.WmiSetBrightness(0,50)'])
proc.wait()

C
chupasaurus, 2016-11-18
@chupasaurus

;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question