Powershell: Connect all hosts to an NFS datastore

This one is a fairly easy one, but I had issues getting the loop to function properly.  This is all one line BTW.

foreach ($vhost in get-cluster <cluster name> | get-vmhost) {$vhost | new-datastore -Nfs -Name <nfs datastore name> -Path "< path >" -NfsHost <hostip>}

Wherever you see this < > these are variables that you will need to change for your environment.

Here is a break down of how the one liner works, with powershell you need to explain commands in a right to left fashion.

So we are looking for vmhosts (get-vmhost) in a certain DRS Cluster (get-cluster) and want the vmhost to be put into a variable ($vhost).

So For Each of the variables ($vhost) run this script:
  • Create a new datastore 
    • it is an NFS datastore
    • here is it's name
    • it's path
    • the server the nfs server resides on
    • and you can add a -readonly switch at the end to make it non-writable.
  • Do this for each $vhost
BTW this works with remove-datastore as well just make sure you use the -confirm:$false switch.