Vmware PowerCli Mount Datastore Script

Here are some useful Powercli Script post building ESXI Hosts:

1. Mount Datastore(s) to ESXI Host PowerCLI script.

We all know mounting NFS Datastores to newly built ESXI hosts can be very tedious task, imagine, you have 20 ESXI Host and at least 5 to 20 multiple NFS Datastores to mount?

 

Mount Network File Share Datastore  – Manual process mount datastore via Vsphere UI will take forever. 

Solution make things easier , using Powercli script – New-Datastore script method- mounting multiple Datastore script code.

Below I will show script method that will save time for VMware Administrators.

I. Mount Datastore Script Configuration.

Save below script code as Mount_Datastore.ps1

############################################################################

#Connect to the VI Server if you are not connected already remove the comment designator
#This script mount NFS Datastore.
‘Reading CSV file’
#location of where datastore.csv is stored.
$vms = Import-CSV C:\Scripts\datastore.csv
‘Reading the contents of the CSV, and for each line execute the following code’
foreach ($vm in $vms){
‘Declaring variables that correspond to the column names in the CSV’
$vmhost = $vm.host
$dhost = $vm.dhost
$dpath = $vm.dpath
$dname = $vm.dnameWrite-Host “Creating Datastore on $vmhost”
‘Datastore mapping’
New-Datastore -nfs -vmhost $vmhost -name $dname -path $dpath -NfsHost $dhost}
‘Ending Script’
#################################################################################
II. Datastore.csv configuration:
#################################################################
host,dhost,dpath,dname
# ESXI host IP or FQDN, datastore server IP or Host,  Data Folder, Datastore Identification ,
Mvp-vs02.mvp.local, 192.168.2.9,/mnt/esx-vg1/app_volume/App_Datastore/App/, App,

 ####################################################################

ESXI host IP or FQDN: Figure#1

exihost

 NFS Datastore IP or Host, Folder, Datastore Identification: Figure #2

datastoreidentifer


 Step 2. Create a Datastore.csv with below configuration.

#################################################################

host,dhost,dpath,dname
# ESXI host IP or FQDN, datastore server IP or Host,  Data Folder, Datastore Identification ,
Mvp-vs02.mvp.local, 192.168.2.9,/mnt/esx-vg1/app_volume/App_Datastore/App/, App,
 ####################################################################
Step 3. Save mountdatastore.zip in a folder called scripts and copy it to local machine.

[embeddoc url=”http://blog.itsysintegration.net/wp-content/uploads/2015/11/mount_datastores.zip” download=”all”]

IV. Step by Step Illustration

1. log into the ESXI host via powercli

a .Launch Powercli and Run as Administrator.

 

openpowercli

B. Log in to the single entity ESXI Host by entering:

Connect-VIserver servername and ESXI host IP or Host
mountdatastoree1

C. Prompt to Input Root Credentials

esxihostroot

D.  Navigate to where Mount Datastore is locate on your desktop via Powercli Command Line.
I.E:  Datastore.cvs and Mount_datastore.ps1

mountdatastorefiles

E. Edit Datastore.csv with all multiple datastores to be mount.

I.E shows datastore.csv should look like
datastorecsvconfg

F. Execute the mount_datastores.ps1 script by typing the .\mount_datastores.ps1

runpwershel

G. Mounted Datastore Successful on ESXI Host.

mounsuccess1

mountscriptworked

Here is an example of multiple Datastores entries added to the datastore.csv.

*Notice the 1 datastore entry was already mounted in 1st script example, the script know it’s already mount and skips it.

New-Datastore : 11/21/2015 11:44:46 PM    New-Datastore        The name ‘App’ already exists.
At C:\Users\richard\Desktop\Powercli\New folder\Mountdatastore\mount_datastores.ps1:16 char:4
+       New-Datastore -nfs -vmhost $vmhost -name $dname -path $dpath -N …
+       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [New-Datastore], DuplicateName
+ FullyQualifiedErrorId : Client20_VmHostServiceImpl_NewNfsDatastore_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.NewDatastore

mountdatastorescript

Newly Datastore Mounted to ESXI Host.

Declaring variables that correspond to the column names in the CSV
Creating Datastore on 192.168.2.233
Datastore mapping
WARNING: Parameter ‘VMHost’ is obsolete. This parameter no longer accepts multiple values.
Name                               FreeSpaceGB      CapacityGB
—-                               ———–      ———-
APP3 NAS2 Datastore                     85.823         186.533
Declaring variables that correspond to the column names in the CSV
Creating Datastore on 192.168.2.233
Datastore mapping
WARNING: Parameter ‘VMHost’ is obsolete. This parameter no longer accepts multiple values.
App4                                   755.104       1,096.120
Declaring variables that correspond to the column names in the CSV
Creating Datastore on 192.168.2.233
Datastore mapping
WARNING: Parameter ‘VMHost’ is obsolete. This parameter no longer accepts multiple values.
OS Datastore                           162.606         323.842
Declaring variables that correspond to the column names in the CSV
Creating Datastore on 192.168.2.233
Datastore mapping
WARNING: Parameter ‘VMHost’ is obsolete. This parameter no longer accepts multiple values.
System Datastore                        31.323          95.297
Ending Script
I. E – ESXI Host with more add datastores mount to it.
exishostmountdatastore
Reference:
New-Datastore PowerCLI Command


Leave a Reply

Your email address will not be published. Required fields are marked *