设为首页 收藏本站
查看: 513|回复: 0

Powershell 操作hyper-v 一般性操作

[复制链接]

尚未签到

发表于 2015-10-8 11:25:13 | 显示全部楼层 |阅读模式
  1. 启动

$hostServer = "shlihu-2k8r2";
$vmName="Win7-Pro2";
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.  关闭
  

$hostServer = "shlihu-2k8r2";
$vmName="Win7-Pro2";
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. 获取虚拟机状态

$hostServer = "shlihu-2k8r2";
$vmName="Win7-Pro2";
$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. 获取snapshot列表

$hostServer = "shlihu-2k8r2";
$vmName="Win7-Pro2";
$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. 恢复snapshot

$hostServer = "shlihu-2k8r2";
$vmName="Win7-Pro2";
$snapshotName ="Win7-Pro2 -VS2010-Silk2010r2-Completed-Install"
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!"
  


  
  

版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-124259-1-1.html 上篇帖子: Powershell 操作hyper-v 一般性操… 下篇帖子: shell 中echo带颜色输出
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表