python 自动创建Hype-V虚拟机脚本
安装模块
pip install pywinrm
脚本如下
#!/usr/bin/env python3
# coding=utf-8
# author:LJX
# describe:一键创建hype-v虚拟机
# createdate:2021.5.26
import winrm
class VM(object):
'''
# 1、查看winrm服务状态。默认没有启动 winrm enumerate winrm/config/listener
# 2、启动服务器(网络类型改为专有网络) winrm quickconfig
# 3、开启防火墙 netsh advfirewall firewall set rule group="Windows 远程管理" new enable=yes
# 4、启动 winrm quickconfig
# 5、查看 winrm enumerate winrm/config/listener
# 5、为winrm service 配置auth winrm set winrm/config/service/auth @{Basic="true"}
# 7、为winrm service 配置加密方式为允许非加密 winrm set winrm/config/service @{AllowUnencrypted="true"}
'''
def __init__(self):
self.host = "xxxx"
self.port = "xxxx"
self.user = "xxxxx"
self.passwd = 'xxxx'
self.linuxfile = r"xxxxxxx"
self.winfile = r"xxxxxx"
self.linuxPath = '''D:\{0}'''.format(self.linuxfile)
self.winPath = '''D:\{0}'''.format(self.winfile)
self.network = "wan"
self.wintest = winrm.Session('http://{0}:{1}/wsman'.format(self.host, self.port), auth=(self.user, self.passwd))
def exec_ps(self, commond,msg):
ret = self.wintest.run_ps(commond)
if ret.status_code == 0:
print(msg + "成功")
elif ret.status_code == 1:
print(msg + "失败")
print(str(ret.std_out, "utf-8"))
def add_vm(self, params):
hostname = params["ip"] +"-"+ params["projectName"]
path = "{0}:\{1}".format(params["diskPath"], hostname)
self.exec_ps(r"new-item -path {0} -type directory".format(path),"创建虚拟机目录")
# 2、
if params["sysType"] == "linux":
self.exec_ps(r'''copy-item {0} {1}'''.format(self.linuxPath, path),"复制系统")
self.exec_ps(
r"New-VM -VHDPath {0}\{1} -MemoryStartupBytes {2}GB -Name {3} -SwitchName {4}".format(path,
self.linuxfile,
params["mem"],
hostname,
self.network),
"创建虚拟机"
)
elif params["sysType"] == "windows":
self.exec_ps(r'''copy-item {0} {1}'''.format(self.winPath, path),"复制系统")
self.exec_ps(
r"New-VM -VHDPath {0}\{1} -MemoryStartupBytes {2}GB -Name {3} -SwitchName {4}".format(path,
self.winfile,
params["mem"],
hostname,
self.network),
"创建虚拟机"
)
self.exec_ps("Set-VMProcessor -VMName {0} -Count {1}".format(hostname, params["core"]),"设置核心数")
if params["otherDisk"]["enable"]:
otherDiskPath = "{0}\{1}".format(path,params["otherDisk"]["name"])
self.exec_ps("New-VHD -Path {0} -SizeBytes {1}GB".format(otherDiskPath,params["otherDisk"]["size"]),"创建磁盘")
self.exec_ps("Add-VMHardDiskDrive -VMName {0} -Path {1}".format(hostname,otherDiskPath),"虚拟机挂载磁盘")
if __name__ == "__main__":
vm_params = {
"projectName": "vmtest",
"ip": "xxxxxx",
"mem": 4,
"core": 4,
"disk": 100,
"diskPath": "F",
"sysType": "linux",
"otherDisk":{
"name": "disk1.vhdx",
"enable": True,
"size": 10,
}
}
vm = VM()
vm.add_vm(vm_params)
分类:
python
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律