Thursday, July 19, 2012

Powershell script to dismount NFS datastore from ESX

There are days in which you are dreaming of everything being completely automated in your job. As an IT administrator these are the days I fire my new PowerGUI and start scripting. Yesterday for instance I had to remove a old NFS datastore which was mounted on a dozen of ESX servers and I felt like I didn't wanted to do it through the vSphere Client. I knew that there are cmdlets for this and decided to take advantage of them. Here follows the script as I imagined it. As you can see I started paying more attention to the style I use in my Powershell scripts, surely because next year I am wishing to enter the 2013 Powershell Scripting Games and I have the felling that the judges will pay a special attention to the look my scripts will have. So I introduced the script with a Synopis containing the syntax to call the script as well as two examples.

The script is structured around three main actions:
  1. Remove the datastore by means of Remove-Datastore cmdlet
  2. Remove the NIC used for vMotion with Remove-VMHostNetworkAdapter
  3. Remove the port group which contained the vMotion interface with Remove-VirtualPortGroup
Also the script takes in parameters, but I won't detail this here because  you can already read about in the script itself.

Unmount the specified NFS datastore and removes the vMotion NIC from a given ESX host.

Connect to an ESX, unmounts the NFS datastore and suppress the vMotion port group.

Mandatory parameters are vc and host_name
Optional parameters are nfsname, vSwitch, vMotionNic and vMotionPG selection.

Specifiy the name of the vSphere server.

Specify the name of the ESX host on which to dismount the NFS datastore.

Specify the name of datastore to dismount.
By default it is nfs.

Specify the name of virtual switch otowhich the vMotion PG is bound.
By default it is vSwitch0.

Specify the name of the vMotion NIC.
By default it is vMotion.

Specify the name of the vMotionport group.
By default it is vMotion.

remove-nfs.ps1 -vc virtualcentername -host_name esxhostname

remove-nfs.ps1 -vc virtualcentername -host_name esxhostname -nfsname nfs -vSwitch vSwitch0 -vMotionNic vMotion -vMotionPG  vMotion

Author: Carlo
Date: 7/19/2012


$vc = $(Read-Host 'Enter vSphere server name'),
$host_name = $(Read-Host 'Enter ESX server name'),
$vSwitch = "vSwitch0",
$vMotionNic = "vMotion",
$vMotionPG = "vMotion"

Add-PSSnapIn VMware.VimAutomation.Core
Connect-VIServer $vc

remove-datastore –datastore $nfsname –vmhost $host_name -Confirm:$false

$esxhost = Get-VMHost -Name $host_name

$vmk = Get-VMHostNetworkAdapter -VMHost $esxhost | where {$_.PortgroupName -eq $vMotionNic}
Remove-VMHostNetworkAdapter -Nic $vmk -Confirm:$false |Out-Null

$vpg = Get-VirtualPortGroup -Name $vMotionPG -VMHost $host_name
Remove-VirtualPortGroup -VirtualPortGroup $vpg -Confirm:$false |Out-Null

The outcome of the execution of this script can be followed on the interface of the vSphere Client:

For more information on managing Datastores using PowerCLI check this article in the VMware knowledge base. You could also check this blog post of mine which has a script to mount a NFS datastore.

Do not hesitate to tell if this post was useful to you. Comment are always welcome.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...