Using VMware PowerCLI (#001)

After long absence, I’m back with some new posts which I hope they become useful one day.

Code #001 (Disconnecting CD Drives):
Last night, while I was looked for making some changes on my hosts in my home lab, I ran “RVTools” for checking VMs if they mounted any ISO images, so I can disconnect them.
I searched for a while and mixed some scripts with hope of making this possible and finally get it work with bellow script.
In order of disconnecting all CD Drives on all VMs in a data center, first of all, connect to the proper VMware vCenter with “Connect VIServer” command, like:

Connect-VIServer -Server vc.kasraeian.com -User USER_NAME -Password PASSWORD

Then run below code for disconnecting all CD Drives:

Get-VM * | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$true

Code #002 (Mounting NFS Share on all hosts):
For some reason I faced the problem with NFS share in my home lab so I decided to remove it and add it back to my hosts (two VMware ESXi 5.0).
If you want to use a GUI method using VMware vSphere Client which seems to be easier, you would end up getting some error because of duplicate name of shared storage on different hosts in the datacenter.
So as the VM may want to be migrated (manually by vMotion or Automatically by DRS), you don’t want to mount the same ISO files each time.
For this purpose it’s possible to use VMware vSphere PowerCLI by connecting to the VMware vCenter by bellow command:

Connect-VIServer -Server vc.kasraeian.com -User USER_NAME -Password PASSWORD

After connecting to the central management (VMware vCenter), it’s possible to run following commands and adding specific NFS share with the same name to all hosts:

$THosts = Get-VMHost
New-Datastore -Nfs -Name NFS_NAME -NfsHost HOST_ADDRESS -Path NFS_PATH -VMHost $THosts

Update [11/15/2012]
For adding new NFS to the all hosts, bellow line can be use as well and if the NFS contains only ISO it’s possible to add it as read only so the data would not be deleted by mistake.

Get-VMHost | %{ New-Datastore -Name NFS_NAME -VMHost $_ -Nfs -NfsHost HOST_ADDRESS -ReadOnly:$true -Path NFS_PATH }

<h1>like bellow example ---&gt;</h1>

Get-VMHost | %{ New-Datastore -Name NFS -VMHost $_ -Nfs -NfsHost 192.168.10.20 -ReadOnly:$true -Path /NFS }

It's your kindness to leave a reply/feedback