无法将“选择对象"识别为内部或外部命令,可运行程序或批处理文件 [英] 'Select-Object' is not recognized as an internal or external command,operable program or batch file

查看:171
本文介绍了无法将“选择对象"识别为内部或外部命令,可运行程序或批处理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过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:

子进程模块提供了更强大的工具来生成新的流程并检索其结果;使用该模块比使用此功能更可取.请参见用子过程模块替换旧功能部分一些有用的食谱的文档.

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.

您还可以对命令进行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)

这篇关于无法将“选择对象"识别为内部或外部命令,可运行程序或批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆