Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'Select-Object' is not recognized as an internal or external command,operable program or batch file

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.

like image 727
谭雪飞 Avatar asked Mar 23 '26 07:03

谭雪飞


1 Answers

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)
like image 145
Aaron Jensen Avatar answered Mar 24 '26 20:03

Aaron Jensen