Friday, March 9, 2012

PowerCLI - Get-DatastoreMountInfo (advanced function) and other commands

How to list datastore mounts, unregister VM's, unmount datastores, disable HA/DRS from a cluster (warning: disabling DRS deletes any resource pools from the cluster), and edit firewall rules.

Requirements for datastore functions: PowerCLI 5.0.1, Powershell v2 and the advanced function from:
http://blogs.vmware.com/vsphere/2012/01/automating-datastore-storage-device-detachment-in-vsphere-5.html
http://communities.vmware.com/docs/DOC-18008

Get-PowerCLIVersion

PowerCLI Version
----------------
   VMware vSphere PowerCLI 5.0.1 build 581491
---------------
Snapin Versions
---------------
   VMware AutoDeploy PowerCLI Component 5.0 build 544967
   VMware ImageBuilder PowerCLI Component 5.0 build 544967
   VMware License PowerCLI Component 5.0 build 544881
   VMware vSphere PowerCLI Component 5.0 build 581435

Get-Datastore | Get-DatastoreMountInfo | Sort Datastore, VMHost | FT -AutoSize

Datastore              VMHost      Lun                                                                        Mounted State   
---------              ------      ---                                                                        ------- -----   
NTNX_datastore_1       192.168.10.1 t10.NUTANIX_shared_data1_1e9999cf2847e0cd727c3014df963e57e975bac8             True Attached
NTNX_datastore_1       192.168.10.2 t10.NUTANIX_shared_data1_1e9999cf2847e0cd727c3014df963e57e975bac8             True Attached
NTNX_datastore_1       192.168.10.3 t10.NUTANIX_shared_data1_1e9999cf2847e0cd727c3014df963e57e975bac8             True Attached
NTNX_datastore_2       192.168.10.1 t10.NUTANIX_shared_data2_6b7ca057ea23a4875fecec0f051eba0c16711f10             True Attached
NTNX_datastore_2       192.168.10.2 t10.NUTANIX_shared_data2_6b7ca057ea23a4875fecec0f051eba0c16711f10             True Attached
NTNX_datastore_2       192.168.10.3 t10.NUTANIX_shared_data2_6b7ca057ea23a4875fecec0f051eba0c16711f10             True Attached
NTNX_datastore_3       192.168.10.1 t10.NUTANIX_shared_data3_e9588e3a87e617fd79ab107b736b54cbd3d55e8e             True Attached
NTNX_datastore_3       192.168.10.2 t10.NUTANIX_shared_data3_e9588e3a87e617fd79ab107b736b54cbd3d55e8e             True Attached
NTNX_datastore_3       192.168.10.3 t10.NUTANIX_shared_data3_e9588e3a87e617fd79ab107b736b54cbd3d55e8e             True Attached
NTNX-1-local-ds-perses 192.168.10.1 t10.ATA_____WDC_WD10EARS2D22Y5B1__________________________WD2DWCAV5M430844    True Attached
NTNX-2-local-ds-perses 192.168.10.2 t10.ATA_____WDC_WD10EARS2D22Y5B1__________________________WD2DWCAV5H180730    True Attached
NTNX-3-local-ds-perses 192.168.10.3 t10.ATA_____WDC_WD10EARS2D22Y5B1__________________________WD2DWCAV5M001915    True Attached

Get-Datastore NTNX_datastore_* | Unmount-Datastore
Unmounting VMFS Datastore NTNX_datastore_1 from host 192.168.10.1...
Exception calling "UnmountVmfsVolume" with "1" argument(s): "The resource 'NTNX_datastore_1' is in use."
At line:101 char:35
+                     $StorageSys.UnmountVmfsVolume <<<< ($DS.ExtensionData.Info.vmfs.uuid);
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
 
Unmounting VMFS Datastore NTNX_datastore_1 from host 192.168.10.2...
Unmounting VMFS Datastore NTNX_datastore_1 from host 192.168.10.3...
Unmounting VMFS Datastore NTNX_datastore_2 from host 192.168.10.1...
Unmounting VMFS Datastore NTNX_datastore_2 from host 192.168.10.2...
Unmounting VMFS Datastore NTNX_datastore_2 from host 192.168.10.3...
Unmounting VMFS Datastore NTNX_datastore_3 from host 192.168.10.1...
Unmounting VMFS Datastore NTNX_datastore_3 from host 192.168.10.2...
Unmounting VMFS Datastore NTNX_datastore_3 from host 192.168.10.3...

The above failed because VM's existed on NTNX_datastore_1 on host 192.168.10.1. To resolve, unregister the VM's with the Remove-VM cmdlet (the -DeletePermanently flag would have deleted from disk):

Get-VM ubuntu-vmfs-0 | Remove-VM -Confirm:$false
Get-VM ubuntu-vmfs-1 | Remove-VM -Confirm:$false

Detach the datastore:

Get-Datastore NTNX_datastore_* | Detach-Datastore

Detaching LUN t10.NUTANIX_shared_data1_1e9999cf2847e0cd727c3014df963e57e975bac8 from host 192.168.10.1...
Detaching LUN t10.NUTANIX_shared_data1_1e9999cf2847e0cd727c3014df963e57e975bac8 from host 192.168.10.2...
Detaching LUN t10.NUTANIX_shared_data1_1e9999cf2847e0cd727c3014df963e57e975bac8 from host 192.168.10.3...
Detaching LUN t10.NUTANIX_shared_data2_6b7ca057ea23a4875fecec0f051eba0c16711f10 from host 192.168.10.1...
Detaching LUN t10.NUTANIX_shared_data2_6b7ca057ea23a4875fecec0f051eba0c16711f10 from host 192.168.10.2...
Detaching LUN t10.NUTANIX_shared_data2_6b7ca057ea23a4875fecec0f051eba0c16711f10 from host 192.168.10.3...
Detaching LUN t10.NUTANIX_shared_data3_e9588e3a87e617fd79ab107b736b54cbd3d55e8e from host 192.168.10.1...
Detaching LUN t10.NUTANIX_shared_data3_e9588e3a87e617fd79ab107b736b54cbd3d55e8e from host 192.168.10.2...
Detaching LUN t10.NUTANIX_shared_data3_e9588e3a87e617fd79ab107b736b54cbd3d55e8e from host 192.168.10.3...
Disabling HA/DRS (warning - all Resource Pools will be removed):

Get-Cluster Perses-Cluster | Set-Cluster -HAEnabled:$false -DrsEnabled:$false -Confirm:$false

Name                           HAEnabled  HAFailover DrsEnabled DrsAutomationLevel  
                                          Level                                     
----                           ---------  ---------- ---------- ------------------  
Perses-Cluster                 False      1          False      Manual              
Checking Firewall Rules for "NFS Client" entry on each host:

Get-VMHost | Get-VMHostFirewallException -Name 'NFS Client'


Name                 Enabled IncomingPorts  OutgoingPorts  Protocols  ServiceRunning 
----                 ------- -------------  -------------  ---------  -------------- 
NFS Client           False                  0-65535        TCP                       
NFS Client           False                  0-65535        TCP                       
NFS Client           True                   0-65535        TCP   
Enabling "NFS Client" entry on the firewall each host:

Get-VMHost | Get-VMHostFirewallException -Name 'NFS Client' | Set-VMHostFirewallException -Enabled:$true

Name                 Enabled IncomingPorts  OutgoingPorts  Protocols  ServiceRunning 
----                 ------- -------------  -------------  ---------  -------------- 
NFS Client           True                   0-65535        TCP                       
NFS Client           True                   0-65535        TCP                       
NFS Client           True                   0-65535        TCP

PowerCLI - Get-VM and Stop-VM

$vCenter = "192.168.10.196"
$vcAcct = "administrator"
$vcPass = "xxxxx"

Connect-VIServer $vCenter -User $vcAcct -Password $vcPass

Get-VM 

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
w2k8_vcenter_1       PoweredOn  1        4096       
NTNX-Ctrl-VM-1-pe... PoweredOn  1        3072       
ubuntu-vmfs-0        PoweredOn  1        1024       
NTNX_vMA             PoweredOn  1        600        
ubuntu-vmfs-1        PoweredOff 1        1024       
NTNX-Ctrl-VM-2-pe... PoweredOn  1        3072       
NTNX-Ctrl-VM-3-pe... PoweredOn  1        3072   

Get-VM | Format-List -Property Name,PowerState

Name       : w2k8_vcenter_1
PowerState : PoweredOn

Name       : NTNX-Ctrl-VM-1-perses
PowerState : PoweredOn

Name       : ubuntu-vmfs-0
PowerState : PoweredOn

Name       : NTNX_vMA
PowerState : PoweredOn

Name       : ubuntu-vmfs-1
PowerState : PoweredOff

Name       : NTNX-Ctrl-VM-2-perses
PowerState : PoweredOn

Name       : NTNX-Ctrl-VM-3-perses
PowerState : PoweredOn

Also:

Get-VM | Format-Table -Property Name,PowerState,NumCpu,MemoryMB
To power off VM:

Get-VM ubuntu-vmfs-0 | Stop-VM -Confirm:$false

Name                 PowerState Num CPUs Memory (MB)
----                 ---------- -------- -----------
ubuntu-vmfs-0        PoweredOff 1        1024       

http://www.vmware.com/support/developer/PowerCLI/PowerCLI501/doc/vsph_powercli_usg501.pdf