问题描述
我想通过powershell的Enable-WindowsOptionalFeature启用IIS.有一个Python程序具有一行代码:
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)')
当我运行python程序时,它说无法将选择对象"识别为内部或外部命令,可操作程序或批处理文件.
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.
推荐答案
我想象os.system
调用正在使用cmd.exe,这将在参数进入powershell.exe之前对其进行处理.管道之前的所有内容都传递给powershell.exe,管道之后的所有内容都传递给cmd.exe程序.没有Select-Object
程序.
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.
使用Python的子流程模块,而不是os.system
,根据system
函数文档中的建议:
Use Python's subprocess module instead of os.system
, per recommendations in the system
function's documentation:
您还可以对命令进行base-64编码,然后将其传递到PowerShell的-EncodedCommand
属性.
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)
这篇关于无法将“选择对象"识别为内部或外部命令,可运行程序或批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!