仕事でVM-ESX Client接続してると、まとめて処理したい、リソース内の現在のイメージの起動状況を一覧で知りたい、となります。そんな時、VMware社がリリースしているVM ESXi(vSphere)管理用PowerShellモジュール「PowerCLI」を使えばいい感じに接続ができます。 ということで、今回はPowerCLIのご紹介です。
PowerCLI概要
素敵ですね!
vSphere PowerCLI: Windows PowerShell interface for managing vSphere
VMware vSphere PowerCLI is a powerful command-line tool that lets you automate all aspects of vSphere management, including network, storage, VM, guest OS and more. PowerCLI is distributed as a Windows PowerShell snapin, and includes over 370 PowerShell cmdlets for managing and automating vSphere and vCloud, along with documentation and samples.
ダウンロード
ダウンロードは、VMware社ホームページからどうぞ。
vSphere PowerCLI: Windows PowerShell interface for managing vSphere
16/Mar/2013現在の最新バージョンは、5.1.0 Release 2です。 My VMware Accountでログインしていれば、Downloadをクリックすると無償でダウンロードできます。
インストール
ダウンロードすることで、.exe形式のインストーラが手に入ります。
- インストーラを実行
- VMware VIXコンポーネントが必要と言われますが、PowerCLIインストール時に自動的にインストール。OKを選択
- インストール画面が起動するのでNextを選択
- Nextを選択
- Licenseをacceptして、Nextを選択
- インストールするコンポーネントを選びNextを選択。ESXiだけならvSphere PowerCLIのみで問題なし
- インストール開始
- インストール完了
- ショートカットの生成でデスクトップに次の名称でショートアイコン作成
VMware vSphere PowerCLI (32-Bit)
VMware vSphere PowerCLI
II. スタートメニュー | VMware | VMware vSphere PowerCLIフォルダで、ユーザーガイドやPowerCLIショートカットができます。
PowerCLI Moduleをインポートする
さて、実はただ単にPowerCLIのショートカットを実行しても起動に失敗します…。 これは、Spaninのインポートをショートカット引数に充ててるのですが、記述が誤っているためです。 また、PowerCLIがPowerShell Consoleでは起動できてもISEでは起動できません。 そこで、PowerISEでPowerCLI Snapinをインポートできないか見てみましょう。 Add-PSSnapinが正常に動作できていません。 また、動作やコマンドレットのインポート自体は、Import-ModuleでのInitialize-powerCLIEnvironment.ps1で完了します。 よって、自作PowerCLI用.ps1に次の行を追加します。
#Add-PSSnapin "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" Import-Module "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
これで、PowerShellシェルホスト画面のPS>がPowerCLIに明示され、コマンドレットも利用できるようになります。
vSphereに接続する
さっそく、ConnectVIServerコマンドレットを利用して接続します。
# Connect to VM ESXi Connect-VIServer -Server "IP or DNSname"
するとユーザークレデンシャルが聞かれますので、管理ユーザーでログインします。
vSphereから切断する
Disconnect-VIServerを利用します。
# Disconnect VM ESXi Disconnect-VIServer
vSphereのVMリストを取得する
Get-VMを利用します。
# List VM Get-VM | Format-Table -AutoSize
vSphereのVMリストをResourcePoolでグルーピング
パイプかますだけです。
# Group VM by resourcePool Property $VMResouece = Get-VM | group resourcePool
Resource Poolの一覧取得
コマンドレットでできます。
# List ResourcePool Get-ResourcePool | Format-Table -AutoSize
VMをResource Pool毎に並び替えて取得
単純にResource Poolでsortして、必要なカラムをselectで取得しています。
# Get-VM Sort by ResourcePool Get-VM ` | sort ResourcePool ` | select ResourcePool, Name, PowerState, "Num CPUs&&;, MemoryGB ` | Format-Table -AutoSize
決まったVMを起動する
簡単なファンクションを書いてみました。
function Start-DefaultVM{ [CmdletBinding()] param( $defaultVM = (, ( "VM1&&;, "VM2&&;, "VM3&&; )) ) begin{ } process{ $defaultVM ` | %{Get-VM -Name $_ } ` | %{Start-VM -VM $_.Name -RunAsync -Confirm} } end{ # Confirm all VM States been stopped if ($null -eq (Get-VM | ? PowerState -eq PoweredON)) { "None of Virtual Machine are running!!&&; } else { "Now below Virtual Machine are running.&&; Get-VM ` | ? PowerState -eq PoweredON ` | sort ResourcePool ` | select ResourcePool, Name, PowerState, "Num CPUs&&;, MemoryGB ` | Format-Table -AutoSize } } }
これで、functionに定めておいたVMのみ起動します。 また、起動状態も最後にホスト画面へだしてくれます。
Start-DefaultVM
全VMを強制的に停止させる
本来はOSからシャットダウンすべきです。参考程度に簡単なファンクションを書いてみました。
function Stop-AllVM{ [CmdletBinding()] param( ) begin{ } process{ # Stop All VM Running on with Confirm Get-VM ` | ? PowerState -eq PoweredOn ` | %{Stop-VM -RunAsync $_ -Confirm} } end{ # Confirm all VM States been stopped if ($null -eq (Get-VM | ? PowerState -eq PoweredON)) { "All Virtual Machine has been stopped!!&&; } else { "Below Virtual Machine are still running.&&; Get-VM ` | ? PowerState -eq PoweredON ` | sort ResourcePool ` | select ResourcePool, Name, PowerState, "Num CPUs&&;, MemoryGB ` | Format-Table -AutoSize } } }
これで、全VMが停止します。
Stop-AllVM
まとめ
vSphere ClientでのGUI管理は便利ですが面倒です……。 是非、VMインスタンスの管理はPowerCLIを使ってPowerShellで自動化しましょう。 定期作業の自動化は、興味があると共に今後も重要な位置を占めていくでしょう…!