2

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.

谭雪飞
  • 21
  • 1
  • 3

3 Answers3

2

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)
Aaron Jensen
  • 25,861
  • 15
  • 82
  • 91
  • thanks, I have done as you said.but it shows another error:
    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 line:1 char:1 + CategoryInfo : ObjectNotFound: CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
    there are some gibberish not shown in the error.
    – 谭雪飞 Aug 04 '15 at 01:59
  • I also use subprocess module. ps_cmdlet = 'powershell.exe Enable-WindowsOptionalFeature -Online -FeatureName $(Get-WindowsOptionalFeature -Online | Where-Object { $_.FeatureName -Like \"IIS-*\"} | Select-Object -ExpandProperty FeatureName)' subprocess.call(ps_cmdlet) but it shows that:
    You must provide a value expression following the '-Like' operator. + ... tureName -Like IIS-*} | Select-Object -ExpandProperty FeatureName) Unexpected token 'IIS-*' in expression or statement. is there a way to solve it?
    – 谭雪飞 Aug 04 '15 at 02:11
0

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.

woodvi
  • 1,898
  • 21
  • 27
-2

If you use sel instead of select it will work

ou_ryperd
  • 2,037
  • 2
  • 18
  • 23
nobody
  • 1