python3使用pywinrm执行Powershell中文乱码
问题描述:python3.6安装pywinrm连接windows server,执行Powershell返回结果中文乱码。解决步骤如下:
1.使用命令chcp
查看windows server编码
> chcp
950 # big5编码
2.修改pywinrm源码__init__.py
def run_cmd(self, command, args=()):
# TODO optimize perf. Do not call open/close shell every time
# shell_id = self.protocol.open_shell() # 原代码,默认codepage=437为英文编码
shell_id = self.protocol.open_shell(codepage=950) # 修改后代码
3.执行Powershell获返回结果,编码输出
r = s.run_ps(ps_shell)
std_out = str(r.std_out, 'big5') # big5编码
print(std_out) # 输出为中文