0

I am writing Python code to get the username of logged user from another computer in a local network. The request works separately both in the command line and in the powershell. However, when I try to enter a request through the subprocess in Python, an error occurs. Tell me what am I doing wrong?

import subprocess, sys

p = subprocess.Popen(["powershell.exe",
              "query session /server:com190"],
              stdout=sys.stdout)
p.communicate()

Result:

*query : The term 'query' is not recognized as the name of a cmdlet, function, s cript file, or operable program. Check the spelling of the name, or if a path w as included, verify that the path is correct and try again. At line:1 char:1

  • query session /server:com190
  • CategoryInfo : ObjectNotFound: (query:String) [], CommandNotFou ndException
  • FullyQualifiedErrorId : CommandNotFoundException*

Commands like ipconfig and ping works perfect in the code.

  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Aug 18 '20 at 14:14
  • I don't use Windows but if you normally execute it in one line `powershell.exe query session /server:com190` then you need list `["powershell.exe", "query", "session", "/server:com190"]` . But if normally you first start `powershell.exe` and later you type `query session /server:com190` then in `Popen` you have to use `["powershell.exe"]` and later use `p.` to send `query session /server:com190` – furas Aug 18 '20 at 14:21
  • What is the need to use `powershell` to run a plain .exe command? If PowerShell can be used, then https://stackoverflow.com/a/23454741/447901 – lit Aug 18 '20 at 14:58
  • https://gallery.technet.microsoft.com/scriptcenter/Get-Terminal-Server-3fadaa68 – lit Aug 18 '20 at 15:01
  • 1
    Does this answer your question? [qwinsta /server:somesrv equivalent in Powershell?](https://stackoverflow.com/questions/23445175/qwinsta-serversomesrv-equivalent-in-powershell) – lit Aug 18 '20 at 15:03

0 Answers0