Script: Copy-NFS-Setup


PowerCLI_Icon_48x48When installing, or adding, new ESXi hosts to your environment you may need to mount NFS shares like the other hosts you have.  Eighter they are used to store virtual machines or iso’s or other media.  Or, maybe you just want to traverse all your host and make all the same NFS mounts to all ESXi hosts like one source host ?

Then this script will help you do that!

 

########################################################################################
## Copy-NFS-Setup.ps1
## Version 1.0
## Auxilium Technology AS, June 2015
## http://www.auxilium.no
##
## Description:
## This script reads NFS setup from a source host, compare with ## a desination host and add it if it doesn't exist.
##
## Script created: June 2015
## Bjørn-Ove Kiil (bok@auxilium.no)
##
## Usage:  .\Copy-NFS-Setup.ps1 <source host> <destination host> ## ## Example: .\Copy-NFS-Setup.ps1 my-esxi-01 my-esxi-02 
##
## Another example: .\Copy-NFS-Setup.ps1 my-esxi-01 *     <-- my-esxi-01 is the source, * is all other hosts in your vCenter!
##
##############################################################################################

## Checking if there are arguments provided
if(!($args[0] -or $args[1]))
{
    Write-Host -Fore Yellow "Missing input arguments!"
	Write-Host -Fore Yellow "Usage:"
	Write-Host -Fore Yellow ".\Copy-NFS-Setup.ps1 <source host> <destination host>"
	Write-Host ""
	Write-Host -Fore Yellow "Example: .\Copy-NFS-Setup.ps1 my-esxi-01 my-esxi-02"
	exit
}
if(!(Get-VMhost $args[0] -ErrorAction SilentlyContinue)) {
	Write-Host -Fore Red "Can not find the source host with the name:" $args[0]
	Exit
}
if(!(Get-VMhost $args[1] -ErrorAction SilentlyContinue)) {
	Write-Host -Fore Red "Can not find the destination host with the name:" $args[1]
	Exit
}
##

$SourceHost = Get-VMHost $args[0]	## Getting the preferred NFS setup from a source host

$DestinationHosts = Get-VMHost $args[1] | ? {$_.Name -ne $SourceHost.Name}	## Defining one or more (*) destination hosts

foreach ($NFS_DS_Source in ($SourceHost | Get-Datastore | ? {$_.Type -eq "NFS"} )) {
	$SharePath = $NFS_DS_Source.RemotePath  ## The shared path on the remote NFS server
	$ShareHost = $NFS_DS_Source.RemoteHost  ## The remote NFS server
	$ShareName = $NFS_DS_Source.Name        ## NFS datastore name

	foreach ( $DestinationHost in $DestinationHosts)
	{
		## Check if the NFS datastore already exists on the desination host
		If ( $DestinationHost | Get-Datastore | ? { $_.Name -eq $ShareName -and $_.Type -eq "NFS"} -ErrorAction SilentlyContinue)
		{
			Write-Host "NFS mount $ShareName already mountet on $DestinationHost" -fore Green
		}
		
		## Check if the NFS datastore doesn't exist, and create it...
		If (!( $DestinationHost | Get-Datastore | ? { $_.Name -eq $ShareName -and $_.Type -eq "NFS"} -ErrorAction SilentlyContinue))
		{
			Write-Host "NFS mount $ShareName does not exist on $DestinationHost" -fore RED
			Write-Host "Mounting: $ShareName from"$ShareHost":"$SharePath -fore Yellow
			New-Datastore -Nfs -VMhost $DestinationHost -Name $ShareName -Path $SharePath -NfsHost $ShareHost | Out-Null
		}
	}
}
Dette innlegget ble publisert i Generell, PowerCLI, VMware og merket med , , , , . Bokmerk permalenken.