Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
管理VM的開關機作業可以經由Powershell 指令進行自動化作業,我要提供的範例是透過ASP.NET 結合 Powershell 來管理VMM, Hyper-V上的 VM 開關機順序。
當需要開關機的VM很多,且有先後順序的需求時(例如: 安裝hotfix),這個範例就可以派上用場。
圖一, Hyper-V管理介面
PowerShell 指令
要達成VM開關機管理所需要的PowerShell有三個,列出VM、執行開機、執行關機,其餘的作業就可以交由ASP.NET 依據業務需求進行調整。
下列範例只需要修改highlight部分即可,其中包含VMM Cluster Name及VM名稱。
# List VM in VMM Cluster
Get-ClusterGroup -Cluster ClusterName | ?{$_.GroupType -eq 'VirtualMachine'}
# Start VM
Get-ClusterGroup -Cluster ClusterName -name vmName | Get-ClusterResource | get-vm | Start-VM
# Stop VM
Get-ClusterGroup -Cluster ClusterName -name vmName | Get-ClusterResource | get-vm | Stop-VM -force
ASP.NET 範例
1. 專案中需要加入System.Management.Automation的參考。
圖二, 加入System.Management.Automation的參考
2. 先列出VMM cluster中的所有VM。
圖三, ASP.NET列出VM
3. 將需要開機或是關機的VM,移至開關機清單中,並設定其順序及間格時間後,設定完成後,就可以執行對應作業。
圖四, 設定開關機順序
Enjoy!