Tuesday 21 June 2016

Powercli New-Datastore script to mount NFS datastores

One of the pain points of building out a ESXi host is usually when you are presenting your NFS storage to an ESXi host. A few datastore is a easy task but when you have to
present 10+ for each of your host it can be a pain. At this point you either have host profiles to help you or you start using powercli to help automate the task.


I will talk through how I will be using the command "new-datastore" cmdlet. The easier way to just add one datastore is to run to command:


new-datastore -nfs -Name Datastore_Name -Path /vol/abc -nfshost IP_Of_NFS_Server


Name - the name that you want to use to help identify the datastore in ESXi host
Path - the export path from your NFS server
Nfshost - ip address of your nfs server


Example
new-datastore -nfs -Name nfsdatastore -path /vol/nfsdatastore -nfshost 192.168.0.100


So you could write a command for each new datastore you wish to add to a host in to a powershell script as shown below:


new-datastore -nfs -Name nfsdatastore01 -path /vol/nfsdatastore01 -nfshost 192.168.0.100
new-datastore -nfs -Name nfsdatastore02 -path /vol/nfsdatastore02 -nfshost 192.168.0.100
new-datastore -nfs -Name nfsdatastore03 -path /vol/nfsdatastore03 -nfshost 192.168.0.100


As we are scripting we should try to store all datastore information in a csv file so that we just loop the command and pull in the required variables from the csv.

Steps to creating the script using powercli version 6.0.0

- Create a csv with the following headings "nfshost", "path" & "name" and save it as a csv, example datastores.csv.
nfshost = your NFS server IP address
path = path to the export of your NFS datastore
name = name you wish to give the datastore in ESXi host







- We now can build the script and below is the script that I have built







Explanation of the script

Line 1 - We are storing the path to the csv file to a variable named $DatastoreCSVPath.

Line 2 - We are using "import-csv" cmdlet to import our CSV file from variable $DatastoreCSVPath to a new variable $DatastoresConf.

Line 3 to 6 - We will use the "foreach" loop method to help us loop through the data stored in variable $DatastoresConf". Line 5 is the main script of the work and you can see I am using the cmdlet "new-datastore".

Three key values are needed which are "Name", "Path" and "Nfshost" to complete a mount of a datastore. We have stored those values in the variable $Datastore as an array of objects now. To use them we would need to reference them by VariableName.ValueName. So with my example it would be $datastore.name or $datastore.path or $datastore.nfshost as those are the only three value names/headers I have used in my csv file.


Line 5 - Command
New-Datastore -Nfs -Name $Datastore.Name -Path $Datastore.Path -Nfshost $Datastore.NfsHost

As you can see I have called the right fields values to the right place within the command I have issued.

That's it. This script is just a really basic one and there can be loads more added to it such as error checking, logging, mounting the datastore as read only etc.

Using script saves time and helps reduce human errors and is repeatable. Go have a play with it.







No comments:

Post a Comment

New Azure KMS IP and domain Addresses for activation

For Windows virtual machines deployed into Azure using marketplace images you may have created rules in your NSG or firewalls to allow the s...