python3
| |
| import os |
| from glob import glob |
| import subprocess as sp |
| |
| |
| class PowerShell: |
| |
| def __init__(self, coding, ): |
| cmd = [self._where('PowerShell.exe'), |
| "-NoLogo", "-NonInteractive", |
| "-Command", "-"] |
| startupinfo = sp.STARTUPINFO() |
| startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW |
| self.popen = sp.Popen(cmd, stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.STDOUT, startupinfo=startupinfo) |
| self.coding = coding |
| |
| def __enter__(self): |
| return self |
| |
| def __exit__(self, a, b, c): |
| self.popen.kill() |
| |
| def run(self, cmd, timeout=15): |
| b_cmd = cmd.encode(encoding=self.coding) |
| try: |
| b_outs, errs = self.popen.communicate(b_cmd, timeout=timeout) |
| except sp.TimeoutExpired: |
| self.popen.kill() |
| b_outs, errs = self.popen.communicate() |
| outs = b_outs.decode(encoding=self.coding) |
| return outs, errs |
| |
| @staticmethod |
| def _where(filename, dirs=None, env="PATH"): |
| """Find file in current dir, in deep_lookup cache or in system path""" |
| if dirs is None: |
| dirs = [] |
| if not isinstance(dirs, list): |
| dirs = [dirs] |
| if glob(filename): |
| return filename |
| paths = [os.curdir] + os.environ[env].split(os.path.pathsep) + dirs |
| try: |
| return next(os.path.normpath(match) |
| for path in paths |
| for match in glob(os.path.join(path, filename)) |
| if match) |
| except (StopIteration, RuntimeError): |
| raise IOError("File not found: %s" % filename) |
| |
| |
| if __name__ == '__main__': |
| |
| with PowerShell('GBK') as ps: |
| outs, errs = ps.run(r'Get-ChildItem G:\test1 -recurse|?{$_.Name -like "test*"}|select Name') |
| |
| |
| print(outs) |
| |
python2
| |
| import os |
| from glob import glob |
| import subprocess as sp |
| |
| |
| class PowerShell: |
| |
| def __init__(self, coding, ): |
| cmd = [self._where('PowerShell.exe'), |
| "-NoLogo", "-NonInteractive", |
| "-Command", "-"] |
| startupinfo = sp.STARTUPINFO() |
| startupinfo.dwFlags |= sp.STARTF_USESHOWWINDOW |
| self.popen = sp.Popen(cmd, stdout=sp.PIPE, stdin=sp.PIPE, stderr=sp.STDOUT, startupinfo=startupinfo) |
| self.coding = coding |
| |
| def __enter__(self): |
| return self |
| |
| def __exit__(self, a, b, c): |
| self.popen.kill() |
| |
| def run(self, cmd): |
| b_cmd = cmd.encode(encoding=self.coding) |
| try: |
| b_outs, errs = self.popen.communicate(b_cmd) |
| except Exception: |
| self.popen.kill() |
| b_outs, errs = self.popen.communicate() |
| outs = b_outs.decode(encoding=self.coding) |
| return outs, errs |
| |
| @staticmethod |
| def _where(filename, dirs=None, env="PATH"): |
| """Find file in current dir, in deep_lookup cache or in system path""" |
| if dirs is None: |
| dirs = [] |
| if not isinstance(dirs, list): |
| dirs = [dirs] |
| if glob(filename): |
| return filename |
| paths = [os.curdir] + os.environ[env].split(os.path.pathsep) + dirs |
| try: |
| return next(os.path.normpath(match) |
| for path in paths |
| for match in glob(os.path.join(path, filename)) |
| if match) |
| except (StopIteration, RuntimeError): |
| raise IOError("File not found: %s" % filename) |
| |
| |
| if __name__ == '__main__': |
| |
| with PowerShell('GBK') as ps: |
| outs, errs = ps.run(r'Get-ChildItem G:\test1 -recurse|?{$_.Name -like "test*"}|select Name') |
| |
| |
| print(outs) |
直接调用 powershell 脚本文件
https://developer.aliyun.com/article/611916?spm=a2c6h.13262185.profile.86.19744749JnL0fq
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!