2
votes

I want to enable IIS by Enable-WindowsOptionalFeature of powershell.there is a python program having one line code:

os.system('powershell.exe Enable-WindowsOptionalFeature -Online -FeatureName $(Get-WindowsOptionalFeature -Online | Where { $_.FeatureName -Like "IIS-*"} | Select-Object -ExpandProperty FeatureName)')

when I run the python program,it says that 'Select-Object' is not recognized as an internal or external command,operable program or batch file.

I search for many ways.But no one can solve this problem,can someone help me with this? Thanks.

3

3 Answers

2
votes

I imagine the os.system call is using cmd.exe, which is mangling your arguments before they get to powershell.exe. Everything before the pipe is passed to powershell.exe, everything after is passed to cmd.exe programs. There is no Select-Object program.

Use Python's subprocess module instead of os.system, per recommendations in the system function's documentation:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

You could also base-64 encode your command and pass that to PowerShell's -EncodedCommand property.

cmd = base64.b64encode( "Enable-WindowsOptionalFeature -Online -FeatureName $(Get-WindowsOptionalFeature -Online | Where { $_.FeatureName -Like "IIS-*"} | Select-Object -ExpandProperty FeatureName)'"
os.system("powershell.exe -EncodedCommand " + cmd)
0
votes

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

Sounds like you're missing an environment variable.


from Command Prompt Error 'C:\Program' is not recognized as an internal or external command, operable program or batch file

If a directory has spaces in, put quotes around it.


from https://superuser.com/questions/571715/windows-command-line-not-recognized-as-an-internal-or-external-command-operab and https://blogs.msdn.microsoft.com/oldnewthing/20071121-00/?p=24433/

It's possible that you have an 'AutoRun' command set in the registry ... try running cmd /d and see if that produces the same message. The /d flag means "don't run AutoRun commands", which makes it perfect for testing this.

The registry values are:

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

Check both. By default, neither should exist. You may wish to fix the command strings in yours, or even delete them entirely.

-1
votes

If you use sel instead of select it will work