System Error on running a powershell command from a python script - python

I am trying to run following python script on windows powershell which is throwing following error
ERROR:
The term 'x86' is not recognized as the name of a cmdlet, function, script file
, or operable program. Check the spelling of the name, or if a path was include
d, verify that the path is correct and try again.
SCRIPT:
import os
import sys
Watchdog_config = 'C:\\Program Files (x86)\\Common Files\\ibm\\icc\\cimom\\data\\wmia.properties'
command1 = "PowerShell -Command \"& {(cat "+Watchdog_config+" )|%{$_ -replace {\"off\",\"on\"}}|set "+Watchdog_config+"}\""
os.system(command1)

Can you try :
Watchdog_config = '\"C:\\Program Files (x86)\\Common Files\\ibm\\icc\\cimom\\data\\wmia.properties\"'
I'am not confortable with python, I just want to enclose path into double quotes "".

Related

I am trying to execute a powershell script in python that references another powershell script

I am attempting to execute a powershell script in python. I was able to do this using a simple 'Hello World' script to prove that it works, but I now need to execute another script written by a retired coworker. I am not very familiar with powershell scripts. All scripts are currently located in the same directory. The script in question invokes another powershell script. Also this does work correctly when invoked from the powershell command line.
The python script looks like this:
# -*- coding: iso-8859-1 -*-
import subprocess, sys
cmd = 'powershell.exe'
dir = 'C:\Agent\\agentStatus.ps1'
p = subprocess.Popen([cmd,
dir],
stdout=sys.stdout)
p.communicate()
The powershell script looks like this:
Write-Host -NoNewLine "Agent service is "
./TSagentService.ps1 -Status
if ((Get-Process "endpoint_runner" -ErrorAction SilentlyContinue) -eq $null) {
Write-Output "Agent is Not Running"
} else {
Write-Output "Agent is Running"
}
The error I get when invoked is this:
./TSagentService.ps1 : The term './TSagentService.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and try again.
At C:\agentStatus.ps1:3 char:1
+ ./TSagentService.ps1 -Status
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (./TSagentService.ps1:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
Do I need to rewrite the original script to get the referenced script to invoke?
While you could replace ./TSagentService.ps1 with $PSScriptRoot/TSagentService.ps1 in the script in order to reliably target a script in the same folder as the enclosing script, there may be other code in the script(s) that assumes that the script's own location is also the working directory, so you're better off setting the working directory to the target script's directory explicitly.
If you're using PowerShell (Core) v6+ with its pwsh.exe CLI, you can take advantage of its new -WorkingDirectory (-wd) parameter to do so; e.g. (using no-shell / cmd.exe / PowerShell syntax):
pwsh -wd c:/path/to -file c:/path/to/script.ps1
In Windows PowerShell, use powershell.exe's -Command (-c) parameter to place a Set-Location call before the invocation of your script; e.g.:
powershell -c "Set-Location c:/path/to; & ./script.ps1"
Applied your Python code, which calls powershell.exe:
# -*- coding: iso-8859-1 -*-
import subprocess, sys, pathlib
cmd = 'powershell.exe'
script = 'C:\Agent\\agentStatus.ps1'
dir = pathlib.Path(script).parent
p = subprocess.Popen(
[
cmd,
'-noprofile',
'-c',
'Set-Location \"{}\"; & \"{}\"'.format(dir, script)
],
stdout=sys.stdout
)
p.communicate()
Note that I've also added -noprofile, which is good practice for non-interactive calls.

'tee' is not recognized as an internal or external command, operable program or batch file

I run some code in python on windows and I get an error of " 'tee' is not recognized as an internal or external command, operable program or batch file. "
result = './model/%s/result.txt' % opt.name # path of result.txt file
os.system('python evaluate_gpu.py | tee -a %s' % result)
I know tee does not exist on windows can anyone help me and write replace code for windows version
Thank all
First of all, you can download UnxUtils from http://sourceforge.net/projects/unxutils/?source=dlp. Then, you need to find tee.exe and move it to the folder of system32 in your pc.

Windows Task Scheduler Bat file Doesn't Recognize Python as internal or external command

I've tried running this .bat file by just double clicking it, and it works. However when I try to run it through Task Scheduler:
CD /D %~dp0
#echo on
python -W ignore DailyRates.py
PAUSE
I get the classic
C:\Windows\system32>CD /D U:\
U:\>python -W ignore DailyRates.py
'python' is not recognized as an internal or external command,
operable program or batch file.
U:\>PAUSE
Press any key to continue . . .
Which means it isn't recognizing python in my PATH, but I am in the correct directory (U: drive)
For anyone curious, I ended up using the explicit path instead of trying to simply call python.
C:\Users\...\...\...\...\...\python.exe -W ignore U:\Sandbox\DailyRates.py

Python error for space in path

In python if I try to give path with space I'm getting error as below
import os
os.system("C:\Program Files (x86)\(application.exe)")
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
How can I give path with space?
You can make it working by using r.
E.g. :
import os
cmd =r'"C:\Program Files (x86)\Java\jre7\bin\java.exe"'
os.system(cmd)

Running external command using python 2.7 in Windows

I am a novice user in python and I am having a problem in executing external command with command-line switch (using python 2.7 in Windows 8 64-bit):
lammpsExe = 'C:/Program Files (x86)/LAMMPS 32-bit 20150403/bin/lmp_serial.exe'
os.system(lammpsExe + " -in in.lmps")
It gives the following error message:
'C Program' is not recognized as an internal or external command, operable program or batch file
It seems that os.system cannot understand the string path for lammpsExe.
Then I tried subprocess.call and replace '/' with '\\' in the path:
lammpsExe = 'C:\\Program Files\\LAMMPS 64-bit 20150330\\bin\\lmp_serial.exe'
subprocess.call([lammpsExe,'-in in.lmps'], shell=True)
But it still doesn't work as the command prompt gives the following warning:
IndentationError: unexpected indent
I suspect the command-line switch '-in' is the problem. I have tried various combination of ", ', \, and /, and I still get error messages.
The subprocess docs has a section titled Replacing os.system():
status = os.system("mycmd" + " myarg")
# becomes
status = subprocess.call("mycmd" + " myarg", shell=True)
I would suggest:
lammpsExe = 'C:\\Program Files\\LAMMPS 64-bit 20150330\\bin\\lmp_serial.exe'
subprocess.call(lammpsExe + ' -in in.lmps', shell=True)
The docs do note that "Calling the program through the shell is usually not required."
I think I have found the solution. Instead of trying to define the path in the python script for the external command, I just need to put the path in the Windows system variables, then calling the command directly in the script:
subprocess.call(['lmp_serial.exe','-in','in.lmps'],shell=True)

Categories