NETWORK ENGINEER BLOG

Tips and Reviews for Engineers

vSphere PowerCLI 覚書

セキュリティポリシーの変更(スクリプトの実行を許可)

PS C:\> Set-ExecutionPolicy RemoteSigned

vCenter サーバーへ接続

PS C:\> Connect-VIServer -User Administrator@vsphere.local -Password P@ssw0rd 192.168.1.10

新規データセンタの作成

PS C:\> $folder = Get-Folder -NoRecursion | New-Folder -Name Folder
PS C:\> New-Datacenter -Location $folder -Name Datacenter | fl

データセンターににホスト(192.168.1.11)を追加

PS C:\> $myServer = Connect-VIServer -Server 192.168.1.10
PS C:\> Add-VMHost -Server $myServer -Name 192.168.1.11 -Location Datacenter -User root -Password password -Force
Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Version
----                 --------------- ---------- ------ ----------- -----------   -------------   ------------- -------
192.168.1.11         Connected       PoweredOn       2         169        6784           1.080           4.000   5.5.0

データセンターに登録済のホストを確認

PowerCLI C:\> Get-VMHost
Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz   MemoryUsageGB   MemoryTotalGB Version
----                 --------------- ---------- ------ ----------- -----------   -------------   ------------- -------
192.168.1.11         Connected       PoweredOn       2          71        6784           1.081           4.000   5.5.0
192.168.1.12         Connected       PoweredOn       2          63        6784           1.081           4.000   5.5.0

192.168.1.* のホストに vSwitch1 を作成

Get-VMhost 192.168.1.* | New-VirtualSwitch -Name vSwitch1 -Nic vmnic1

vSwitch 情報の確認

PowerCLI C:\> Get-VirtualSwitch -VMHost 192.168.1.11 | fl
Id                : key-vim.host.VirtualSwitch-vSwitch0
Key               : key-vim.host.VirtualSwitch-vSwitch0
Name              : vSwitch0
NumPorts          : 1536
NumPortsAvailable : 1531
Nic               : {vmnic0}
Mtu               : 1500
VMHostId          : HostSystem-host-30
VMHost            : 192.168.1.11
VMHostUid         : /VIServer=administrator@vsphere.local@192.168.1.10:443/VMHost=HostSystem-host-30/
Uid               : /VIServer=administrator@vsphere.local@192.168.1.10:443/VMHost=HostSystem-host-30/VirtualSwitch=key-vim.host.VirtualSwitch-vSwitch0/
ExtensionData     : VMware.Vim.HostVirtualSwitch

vSwitch ポートグループ情報の確認

PowerCLI C:\> Get-VirtualPortGroup -VMHost 192.168.1.11 | ft -autosize
Name               Key                                       VLanId PortBinding NumPorts
----               ---                                       ------ ----------- --------
VM Network         key-vim.host.PortGroup-VM Network         0
Management Network key-vim.host.PortGroup-Management Network 0

vSwitch1 に vMotion 用の vmkernel ポートを作成

PowerCLI C:\> $vmhost = Get-VMHost -Name 192.168.1.11
PowerCLI C:\> $myVirtualSwitch = Get-VirtualSwitch -VMHost $vmhost -Name vSwitch1
PowerCLI C:\> New-VMHostNetworkAdapter -VMHost $vmhost -PortGroup vMotion -VirtualSwitch $myVirtualSwitch -IP 172.16.1.11 -SubnetMask 255.255.255.0 -VMotionEnabled 1

vmkernel ポートの情報確認

PowerCLI C:\> Get-VMHostNetworkAdapter -VMHost 192.168.1.11 | Where-Object{$_.Name -eq "vmk1"} | fl
VMotionEnabled               : True
FaultToleranceLoggingEnabled : False
ManagementTrafficEnabled     : False
IPv6                         : {fe80::250:56ff:fe6d:b200/64}
AutomaticIPv6                : False
IPv6ThroughDhcp              : False
IPv6Enabled                  : False
Mtu                          : 1500
VsanTrafficEnabled           : False
PortGroupName                : vMotion
Id                           : key-vim.host.VirtualNic-vmk1
VMHostId                     : HostSystem-host-30
VMHost                       : 192.168.1.11
VMHostUid                    : /VIServer=administrator@vsphere.local@192.168.1.10:443/VMHost=HostSystem-host-30/
DeviceName                   : vmk1
Mac                          : 00:50:56:6d:b2:00
DhcpEnabled                  : False
IP                           : 172.16.1.11
SubnetMask                   : 255.255.255.0
Uid                          : /VIServer=administrator@vsphere.local@192.168.1.10:443/VMHost=HostSystem-host-30/HostVMKernelVirtualNic=key-vim.host.VirtualNic-vmk1/
Name                         : vmk1
ExtensionData                : VMware.Vim.HostVirtualNic

ホストのネットワークアダプタ状態の確認

PowerCLI C:\> Get-VMHostNetworkAdapter -VMHost 192.168.1.* | ft -autosize
Name   Mac               DhcpEnabled IP           SubnetMask    DeviceName
----   ---               ----------- --           ----------    ----------
vmnic0 00:0c:29:b7:42:58 False                                      vmnic0
vmnic1 00:0c:29:b7:42:62 False                                      vmnic1
vmnic2 00:0c:29:b7:42:6c False                                      vmnic2
vmnic3 00:0c:29:b7:42:76 False                                      vmnic3
vmk0   00:0c:29:b7:42:58 False       192.168.1.11 255.255.255.0       vmk0
vmk1   00:50:56:6d:b2:00 False       172.16.1.11  255.255.255.0       vmk1
vmnic0 00:0c:29:04:5e:52 False                                      vmnic0
vmnic1 00:0c:29:04:5e:5c False                                      vmnic1
vmnic2 00:0c:29:04:5e:66 False                                      vmnic2
vmnic3 00:0c:29:04:5e:70 False                                      vmnic3
vmk0   00:0c:29:04:5e:52 False       192.168.1.12 255.255.255.0       vmk0
vmk1   00:50:56:68:0f:b6 False       172.16.1.12  255.255.255.0       vmk1

分散スイッチの作成

PowerCLI C:\> $myDatacenter = Get-Datacenter -Name Datacenter
PowerCLI C:\> New-VDSwitch -Name "VDSwitch" -Location $myDatacenter -NumUplinkPorts 1

分散スイッチへホストを追加

PowerCLI C:\> Get-VDSwitch -Name "VDSwitch" | Add-VDSwitchVMHost -VMHost "192.168.1.11", "192.168.1.12"

分散スイッチのアップリンクポートへ物理アダプタを追加

PowerCLI C:\> $vmhostNetworkAdapter = Get-VMHost "192.168.1.*" | Get-VMHostNetworkAdapter -Physical -Name vmnic2
PowerCLI C:\> Get-VDSwitch "VDSwitch" | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter

分散スイッチへポートグループを追加

Get-VDSwitch -Name "VDSwitch" | New-VDPortgroup -Name "VDPortGroup"

分散スイッチ情報の確認

PowerCLI C:\> Get-VDSwitch -VMHost 192.168.1.*
Name                           NumPorts   Mtu        Version  Vendor
----                           --------   ---        -------  ------
VDSwitch                       8          1500       5.5.0    VMware

分散スイッチポートグループ名の変更

Get-VDPortgroup -Name "VDPortGroup" | Set-VDPortgroup -Name "VDPortGroup2"

仮想マシンの作成。Guest ID の詳細は こちら

PowerCLI C:\> $myTargetVMHost = Get-VMHost -Name 192.168.1.11
PowerCLI C:\> New-VM -Name MyVM1 -ResourcePool $myTargetVMHost -Datastore Datastore1 -NumCPU 1 -MemoryGB 1 -DiskGB 40 -NetworkName "VM Network" -CD -DiskStorageFormat Thin -GuestID centosGuest

仮想マシンを OVF から作成

PowerCLI C:\> $myDatastore = Get-Datastore -Name "Datastore1"
PowerCLI C:\> $vmHost = Get-VMHost -Name "192.168.1.11"
PowerCLI C:\> $vmHost | Import-vApp -Name dc1-otv -Source "c:\csr1000v-universalk9.03.11.00.S.154-1.S-std-C1-M2560-N3-DS8.ova" -Datastore $myDatastore -Force

仮想マシンに適用しているポートグループの変更

get-vm -name MyVM1 | Get-NetworkAdapter | Where-Object{$_.NetworkName -eq "VM Network"} | Set-NetworkAdapter -Portgroup VDPortGroup

x-vMotion の実行

PowerCLI C:\> Move-VM -VM MyVM1 -Destination 192.168.1.12 -Datastore datastore2

vCenter サーバーを切断

PowerCLI C:\> Disconnect-VIServer -Server 192.168.1.10 -Confirm:$False
広告を非表示にする