macOS で PowerCLI を使えるようにする
はじめに
PowerCLI とは、vSphere 環境を管理するために PowerShell に導入して利用するツールです。
本記事では、「macOS に PowerCLI を導入する方法」をご紹介します。
PowerShell のインストール
まず、macOS に PowerShell をインストールします。
macOS 10.13 以降が必要となります。sw_vers
コマンドでバージョン確認が可能です。
$ sw_vers
ProductName: macOS
ProductVersion: 11.4
BuildVersion: 20F71
以下のコマンドでPowerShell のインストールが完了します(便利ですね)
$ brew install --cask powershell
==> Installing Cask powershell
==> Running installer for powershell; your password may be necessary.
Package installers may write to any location; options such as `--appdir` are ignored.
Password:
installer: Package name is PowerShell - 7.1.3
installer: Installing at base path /
installer: The install was successful.
🍺 powershell was successfully installed!
無事に 7.1.3 というバージョンの PowerShell がインストールできました。
pwsh
コマンドでPowerShell が起動します。
$ pwsh
PowerShell 7.1.3
Copyright (c) Microsoft Corporation.
https://aka.ms/powershell
Type 'help' to get help.
PS >
PowerCLI のインストール
続いて、PowerCLI をインストールします。
以下のコマンドで PowerShell Gallery から PowerCLI をインストールしています。
PowerShell Gallery のレポジトリを信頼するか尋ねられるので、Y (信頼する) を入力します。
PS > Install-Module -Name VMware.PowerCLI -RequiredVersion 12.2.0.17538434
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the
Set-PSRepository cmdlet. Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"): Y
インストール後はこちらのコマンドで確認できます。
私の環境では、意外と時間がかかったので気長に待ちましょう。
PS > Get-Module VMware* -ListAvailable
最後に、vCenter に接続してみます。
PS > Connect-VIServer -Server xxx.xx.xx.xx -Username 'xxxxx@vsphere.local' -Password 'xxxxx' -force
WARNING: Please consider joining the VMware Customer Experience Improvement Program, so you can help us make PowerCLI a better product. You can join using the following command:
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true
VMware's Customer Experience Improvement Program ("CEIP") provides VMware with information that enables VMware to improve its products and services, to fix problems, and to advise you on how best to deploy and use our products. As part of the CEIP, VMware collects technical information about your organization’s use of VMware products and services on a regular basis in association with your organization’s VMware license key(s). This information does not personally identify any individual.
For more details: type "help about_ceip" to see the related help article.
To disable this warning and set your preference use the following command and restart PowerShell:
Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true or $false.
Name Port User
---- ---- ----
xxx.xx.xx.xx 443 VSPHERE.LOCAL\xxxxxxxx
CEIP へ参加を促す Warning がでるので以下のコマンドで参加、もしくは不参加の設定を行っておきます。
PS > Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $false
Perform operation?
Performing operation 'Update PowerCLI configuration.'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "Y"): Y
Get-VM
コマンドで仮想マシンの確認ができます。
PS > Get-VM
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
uag PoweredOff 2 4.000
vCLS (1) PoweredOn 1 0.125
vcsa01 PoweredOn 2 12.000
lin01 PoweredOff 1 3.000
ws01 PoweredOn 4 8.000
vyos01 PoweredOff 1 1.000
vyos02 PoweredOff 1 1.000
win01 PoweredOff 2 4.000
truenas PoweredOff 2 8.000
切断は以下のように行います。
PS > Disconnect-VIServer -Server ‘xxx.xxx.xxx.xx’ -Confirm:$False
はい、ここまでで macOS でも問題なく PowerCLI が利用できる環境が整ったと思います。
次回以降のブログでは PowerCLI を使った自動化の話もしていければなと考えています!