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

3 comments:

  1. awesome script, helps me automate stuff better, BTW - I am interested in understanding - how are you making use of this Snap-in. Are you automating stuff? Please tell me more about it!

    Another question I was - what is the best practice around auto-loading the snap-ins when you pull up a powerCLI or run a PS script at a scheduled time.

    Kindly comment
    Thanks,
    Nirav

    ReplyDelete
    Replies
    1. Hi Nirav,

      To auto-load the snap in I usually just put the following code at the top of the script:

      # Add snap-in if required
      if ( (Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null ) {
      add-pssnapin VMware.VimAutomation.Core
      }
      else {
      Write-Host "Snap in already added."
      }

      Thanks,
      Laura

      Delete
  2. I have installed Powercli 5.1 and it is missing some of the commands that were present in 5.0. Get-DatastoreMountInfo seems to be missing. Do you know of a replacement command because I am not seeing one in the list.

    ReplyDelete