点击上方“IT那活儿”公众号--专注于企业全栈运维技术分享,不管IT什么活儿,干就完了!!!
简 介
Windows 远程管理 (WinRM) 是 WS-Management 协议的 Microsoft 实现,该协议是标准简单对象访问协议 (基于 SOAP) 的防火墙友好协议,允许不同供应商的硬件和操作系统之间进行互操作。
WS-Management协议规范为系统提供了一种跨 IT 基础结构访问和交换管理信息的常用方法。 WinRM 和 智能平台管理接口 (IPMI) 标准,以及 事件收集器服务 是称为 硬件管理的功能集的组件。
官网链接-《Windows 远程管理 - Win32 apps | Microsoft Learn》::
https://learn.microsoft.com/zh-cn/windows/win32/winrm/ws-management-protocol
配置winrm
# 查看是否开启winrm
sc query WinRM
RUNNING: 服务正在运行。
STOPPED: 服务已停止。
START_PENDING, STOP_PENDING, PAUSE_PENDING, CONTINUE_PENDING: 服务正在启动、停止、暂停或继续过程中。
# 获取winrm详情配置
winrm get winrm config
# 获取winrm监听信息
winrm enumerate winrm config/listener
# 修改windows http默认端口
winrm set winrm config/listener?Address=*+Transport=HTTP @{Port="5985"}复制
获取通过服务进行查询,如下图:

调整对外防火墙配置:

对接python
cmd_script变量中存放远程windows具体执行的命令:
pip install pypsrp # 安装环境
#-*-coding:utf-8-*-
# author: lyc
import pypsrp.client
# 远程机器的IP地址、用户名和密码
remote_host = 'XX.XX.XX.XX'
remote_username = 'administrator'
remote_password = 'xxxxxx'
remote_port = '8888'
session = pypsrp.client.Client(server=remote_host, username=remote_username, password=remote_password, port=remote_port, ssl=False)
cmd_script = 'ipconfig'
resule = session.execute_ps(cmd_script)
#resule = session.execute_cmd(cmd_script)
print (resule[0])复制
powershell脚本实现
基于powershell实现执行的功能:
# 检查文件MD5
cmd_script = 'certutil -hashfile {} MD5 | Select-Object -Index 1'.format(remote_file_path)
# 文件解压
cmd_script = 'Expand-Archive -Path "{}" -DestinationPath "{}"'.format(file_path, decompression_path)
# 获取压缩包文件名
cmd_script = '''
$shell = New-Object -ComObject Shell.Application
$zipFile = $shell.NameSpace('{}')
$zipFile.Items() | ForEach-Object {{ $_.Name }}
'''.format(file_path)
# 删除文件
cmd_script = 'Remove-Item -Path "{}" -Force'.format(del_file_path)复制

本文作者:刘玉翀(上海新炬中北团队)
本文来源:“IT那活儿”公众号

文章转载自IT那活儿,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
81个Windows命令,带你解锁隐藏技能!
小周的数据库进阶之路
30次阅读
2025-04-15 09:56:41
Windows Server系统怎么选择?
云知识CLOUD
23次阅读
2025-04-21 20:27:42
《解锁Windows Server用户权限管理秘籍,打造高效安全办公环境》
程序员阿伟
17次阅读
2025-04-26 22:12:58
Windows 下 Coco AI 全网最全部署实战指南
铭毅天下Elasticsearch
15次阅读
2025-04-07 09:46:41
数据库部署的真相:Linux和Windows,谁更适合你的业务?
青年数据库学习互助会
15次阅读
2025-04-03 09:58:54