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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With