Powershell 实现 Hyper-V 虚拟机 管理
2011-09-17 17:15
495 查看
本文包括使用powershell启动、关闭Hyper-V虚拟机,及获得虚拟机状态、虚拟机快照、根据快照回滚虚拟机。
1. 启动
2. 关闭
3. 获得虚拟机状态 (状态码参考:http://msdn.microsoft.com/en-us/library/cc136822(VS.85).aspx)
4. 获得虚拟机快照列表
5. 根据快照回滚虚拟机
1. 启动
param ( [string]$hostServer = $(throw "param -host server is required."), [string]$vmName = $(throw "param -virtual machine name is required.") ) function GetImageState { param ( [string]$image = $(throw "param -image is required."), [string]$hostServer = $(throw "param -hostserver is required.") ) $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer; foreach ($virtualMachine in $virtualMachines) { if ($image -ieq $virtualMachine.ElementName) { return $virtualMachine.EnabledState; } } throw "Cannot get the state of image [$image] on host server [$hostServer]"; } function WaitImageToState { param ( [string]$image = $(throw "param -image is required."), [string]$hostServer = $(throw "param -hostserver is required."), [int]$state = $(throw "param -$state is required."), [int]$timeOut = $(throw "param -$timeOut is required.") ) do { $timeOut = $timeOut - 5; sleep (5); $currentState = GetImageState -image:$image -hostserver:$hostServer; if ($currentState -eq $state) { return; } if ($timeOut -le 0) { throw "Wait for image [$image] to state [$state] time out."; } }while($true); } "Start up the virtual machine..." + $hostServer Set-ExecutionPolicy -ExecutionPolicy RemoteSigned $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer; $virtualMachineRun = 2; foreach ($virtualMachine in $virtualMachines) { if($virtualMachine.ElementName -eq $vmName) { $result = $virtualMachine.RequestStateChange($virtualMachineRun); if ($result.ReturnValue -ne 4096) { throw "Failed to send start request to VM - " + $virtualMachine; } WaitImageToState -image:$virtualMachine.ElementName -hostserver:$hostServer -state:$virtualMachineRun -timeOut:60; } } $vmName + "startup Finished!"
2. 关闭
param ( [string]$hostServer = $(throw "param -host server is required."), [string]$vmName = $(throw "param -virtual machine name is required.") ) function GetImageState { param ( [string]$image = $(throw "param -image is required."), [string]$hostServer = $(throw "param -hostserver is required.") ) $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer; foreach ($virtualMachine in $virtualMachines) { if ($image -ieq $virtualMachine.ElementName) { return $virtualMachine.EnabledState; } } throw "Cannot get the state of image [$image] on host server [$hostServer]"; } function WaitImageToState { param ( [string]$image = $(throw "param -image is required."), [string]$hostServer = $(throw "param -hostserver is required."), [int]$state = $(throw "param -$state is required."), [int]$timeOut = $(throw "param -$timeOut is required.") ) do { $timeOut = $timeOut - 5; sleep (5); $currentState = GetImageState -image:$image -hostserver:$hostServer; if ($currentState -eq $state) { return; } if ($timeOut -le 0) { throw "Wait for image [$image] to state [$state] time out."; } }while($true); } "Shutdown the virtual machine..." Set-ExecutionPolicy -ExecutionPolicy RemoteSigned $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer; $virtualMachineRun = 3; foreach ($virtualMachine in $virtualMachines) { if($virtualMachine.ElementName -eq $vmName) { $result = $virtualMachine.RequestStateChange($virtualMachineRun); if ($result.ReturnValue -ne 4096) { throw "Failed to send start request to VM - " + $virtualMachine; } WaitImageToState -image:$virtualMachine.ElementName -hostserver:$hostServer -state:$virtualMachineRun -timeOut:60; } } $vmName + "Shutdown Finished!"
3. 获得虚拟机状态 (状态码参考:http://msdn.microsoft.com/en-us/library/cc136822(VS.85).aspx)
param ( [string]$hostServer = $(throw "param -hostserver is required."), [string]$vmName = $(throw "param -vmName is required.") ) $status = -1 $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer foreach ($virtualMachine in $virtualMachines) { if ($vmName -ieq $virtualMachine.ElementName) { $status = $virtualMachine.EnabledState break } } switch($status) { -1 {throw "Cannot get the state of vmName [$vmName] on host server [$hostServer]"} 2 {"Running."} 3 {"Closed."} 32768 {"Paused."} 32769 {"Saved."} 32770 {"Starting."} 32773 {"Saving."} 32774 {"Stopping."} 32776 {"Pausing."} 32777 {"Restoring."} default {"Unknow Status."} }
4. 获得虚拟机快照列表
param ( [string]$hostServer = $(throw "param -host server is required."), [string]$vmName = $(throw "param -virtual machine name is required.") ) $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer; foreach ($virtualMachine in $virtualMachines) { if($virtualMachine.ElementName -eq $vmName) { # get snapshots of the virtual machine $queryStr = "SELECT * FROM Msvm_VirtualSystemSettingData WHERE SystemName='" + $virtualMachine.Name + "' and SettingType=5"; $snapShots = Get-WmiObject -Query $queryStr -Namespace "root\virtualization" -ComputerName $hostServer; foreach ($aSnapShot in $snapShots) { $aSnapShot.ElementName [System.DateTime]::ParseExact($aSnapShot.CreationTime.Substring(0,14), "yyyyMMddHHmmss", $null).ToLocalTime().ToString() $aSnapShot.Notes } } }
5. 根据快照回滚虚拟机
param ( [string]$hostServer = $(throw "param -hostServer is required."), [string]$vmName = $(throw "param -virtual machine name is required."), [string]$snapshotName = $(throw "param -snapshotName is required.") ) function GetImageState { param ( [string]$image = $(throw "param -image is required."), [string]$hostServer = $(throw "param -hostserver is required.") ) $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer; foreach ($virtualMachine in $virtualMachines) { if ($image -ieq $virtualMachine.ElementName) { return $virtualMachine.EnabledState; } } throw "Cannot get the state of image [$image] on host server [$hostServer]"; } function WaitImageToState { param ( [string]$image = $(throw "param -image is required."), [string]$hostServer = $(throw "param -hostserver is required."), [int]$state = $(throw "param -$state is required."), [int]$timeOut = $(throw "param -$timeOut is required.") ) do { $timeOut = $timeOut - 5; sleep (5); $currentState = GetImageState -image:$image -hostserver:$hostServer; if ($currentState -eq $state) { return; } if ($timeOut -le 0) { throw "Wait for image [$image] to state [$state] time out."; } }while($true); } "Rollback the virtual machine..." $virtualMachines = Get-WmiObject -Class "MSVM_ComputerSystem" -Namespace "root\virtualization" -ComputerName $hostServer; $virtualSystemService = Get-WmiObject -Class "Msvm_VirtualSystemManagementService" -Namespace "root\virtualization" -ComputerName $hostServer; foreach ($virtualMachine in $virtualMachines) { if($virtualMachine.ElementName -eq $vmName) { # get snapshot of the virtual machine $queryStr = "SELECT * FROM Msvm_VirtualSystemSettingData WHERE SystemName='" + $virtualMachine.Name + "' and SettingType=5"; $snapShots = Get-WmiObject -Query $queryStr -Namespace "root\virtualization" -ComputerName $hostServer; foreach ($aSnapShot in $snapShots) { # revert to specified snapshot if ($aSnapShot.ElementName -ieq $snapshotName) { # apply snapshot $result = $virtualSystemService.ApplyVirtualSystemSnapShot($virtualMachine.__PATH, $aSnapShot.__PATH); if ($result.ReturnValue -ne 0) { throw "Failed to apply snapshot ["+$snapshotName+"] to VM ["+$image+"]"; } WaitImageToState -image:$virtualMachine.ElementName -hostserver:$hostServer -state:32769 -timeOut:120; } } } } "Finished rollback!"
相关文章推荐
- Hyper-V 2016 系列教程40 使用 PowerShell 实现虚拟机自动化和管理虚拟机
- C# 实现 Hyper-V 虚拟机 管理
- C# 实现 Hyper-V 虚拟机 管理
- 使用路由和远程访问服务为Hyper-V中虚拟机实现NAT上网 推荐
- Hyper-V虚拟机实现无群集无共享的实时迁移
- 使用PowerShell管理Hyper-v 推荐
- 在Windows 7中轻松管理Virtual PC/Hyper-V虚拟机磁盘文件VHD
- libvirt工具实现虚拟机管理
- Copy file cross Hyper-v Server and VMs 实现虚拟机和主机的文件分享
- Hyper-V应用指南之4-虚拟机管理[转]
- VMWARE VICLIENT实现虚拟机独立管理
- 【WINDOWS】hyper-v下自动创建VM_powershell脚本实现_powershell简介
- [置顶] Linux实现KVM+QEMU+libvirt的虚拟机环境 并使用virsh对虚拟机进行管理
- powershell远程管理服务器磁盘空间的实现代码
- Hyper-V的管理与使用之二:新建虚拟机
- 使用Windows Azure PowerShell远程管理Windows Azure虚拟机
- 使用subversion实现虚拟机中linux和windows宿主机代码共享管理
- 详解实现hyper-v虚拟机克隆的方法
- 实现Hyper-V 虚拟机在不同架构的处理器间迁移
- java.lang.management包——实现监视和管理 Java 虚拟机