2

Is anyone able to explain how the Windows/Open Actions work?

And also the python console?

I have a powerscript .ps1 file I was wanting to launch from either of these options and it just doesn't seem to work.

If I try to just run the following as a Windows Action.

powershell

I get the error message "Unable to run command powershell".

However if I try to run cmd I don't get an error and I know it is doing something in the background because running the following Windows Action launches notepad.

cmd /c ("notepad")

In the python console if I run the following script then notepad happily pops up.

import subprocess
subprocess.call(['notepad'])

But if I replace 'notepad' with 'powershell' I get the following error.

Traceback (most recent call last):
File "C:\OSGEO4~1\apps\Python36\lib\code.py", line 91, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "C:\OSGEO4~1\apps\Python36\lib\subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\OSGEO4~1\apps\Python36\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\OSGEO4~1\apps\Python36\lib\subprocess.py", line 990, in
_execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

I expect this has something to do with an OSGEO4 shell that is running in the background or that is called by these commands?

I haven't been able to find detailed information about or good examples of how these functions work outside of doing things within QGIS, where I want to use QGIS to run certain functions outside of QGIS (via powershell scripts), I assume it is possible?

Also if someone does run powershell scripts from within QGIS could you please explain how it is done? And if you feed it variables I'd be very interested in that too (though assuming I can run ps1 scripts I assume the easiest way would be to output the variable into a text file and just have the ps1 script read in the text in there, or output a ps1 file and call it from another ps1 file?

1

In QGIS, using the same commands, I get the same error as you. However it will work if you put in the full path to PowerShell:

import subprocess
subprocess.call(['c:\\windows\\system32\\WindowsPowerShell\\v1.0\\powershell'])
  • Thanks for this, I ended up calling a batch file which called PowerShell which ran a PowerShell script which ran two PowerShell scripts...quite convoluted really, I'm sure there must be an easier way. When I get home from work I'll do a full write up of the issues I was having and how I solved it, others that got stuck like me might find it useful or someone might come along and show me "the right way". I'm still interested in understanding why some commands work and others don't, Perhaps the OSGEO4 shell isn't reading from the PATH environment variable? – bdc Sep 23 '18 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.