Script for restarting vSAN services on ESXi hosts

On a recent project we discovered an issue with vSAN which necessitated restarting the vSAN services on all of the hosts in the affected vSAN cluster.

So digging into the bag brought out the script detailed in an earlier post for updating Likewise settings on ESXi hosts http://www.caenotech.co.uk/vmware/script-for-updating-likewise-and-registry-configuration-on-esxi/

This is an adaption of that script, advanced and simplified for restarting vSAN Services, which might be more applicable and useful for reuse in the future without the complexity of escape characters and spaces within the Likewise registry

This script is designed to run on a Windows Server 2012 R2 server using Powershell 4.0 along with plink (a command-line interface to the PuTTY back ends) which can be downloaded from https://www.putty.org

If you run the script it will produce a csv file called hosts.csv in the folder c:\temp to be used as a template. Running the script again with the csv file completed will proceed to run the script connecting to each host listed in the csv file and then output a csv file called export.csv in the c:\temp\ folder with the results.

$hostList = "c:\temp\Hosts.csv"


if (-NOT (Test-Path $hostList)) {


	[pscustomobject]@{ Hostname =  'Host1'; Password = 'Password1' } | Export-Csv -Path  $hostList -Append -NoTypeInformation

	" "
	"----------------------------------------------"
	"-------- Host file CSV does not exist --------"
	"-- Creating empty file in" + $hostList + " --"
	"---- Please complete and run script again ----"
	"----------------------------------------------"
	" "		



	exit
}

$csv = Import-Csv $hostList


$table=@()


foreach($item in $csv)
	{
		" "
		"-------------------------------------------"
		"-- Hostname = "+$($item.Hostname)+" --"
		"-------------------------------------------"
		" "		
		
		$plink = '"C:\Program Files\PuTTY\plink.exe" -v -batch -pw'
        $plinkCachekey = 'echo y | "C:\Program Files\PuTTY\plink.exe" -pw'
		$esxUser = ' root@'
        $exitCmd = ' exit'
		$serviceCmd = ' /etc/init.d/vsanmgtd status'
		$serviceCmd2 = ' /etc/init.d/vsanmgtd restart'
		$serviceCmd3 = ' /etc/init.d/vsanvpd status'
		$serviceCmd4 = ' /etc/init.d/vsanvpd restart'
		
		$grep = '| grep '
		$quote = '"'

        $plinkCacheKeyCmd = $plinkCachekey + " " + $($item.Password) + $esxUser + $($item.Hostname) + $exitCmd


		$serviceRestartCmd = $plink + " " + $($item.Password) + $esxUser + $($item.Hostname) + $serviceCmd
		$serviceRestartCmd2 = $plink + " " + $($item.Password) + $esxUser + $($item.Hostname) + $serviceCmd2
		$serviceRestartCmd3 = $plink + " " + $($item.Password) + $esxUser + $($item.Hostname) + $serviceCmd3
		$serviceRestartCmd4 = $plink + " " + $($item.Password) + $esxUser + $($item.Hostname) + $serviceCmd4


		" "
		"----------------------------------"
		"-------- Cacheing SSH Key --------"
		"----------------------------------"
		" "
        
        Invoke-Expression -command 'cmd.exe /c $plinkCacheKeyCmd'
        

			" "
			"----------------------------------"
			"-------- Status vsanmgtd Service -------"
			"----------------------------------"
			" "

			$StatusResult = Invoke-Expression -command 'cmd.exe /c $serviceRestartCmd'
			$StatusResult1 = $StatusResult
			
			" "
			"----------------------------------"
			"------- Restarting vsanmgtd Service -------"
			"----------------------------------"
			" "

			Invoke-Expression -command 'cmd.exe /c $serviceRestartCmd2'
			$table += $row
		
					" "
			"----------------------------------"
			"-------- Status vsanvpd Service -------"
			"----------------------------------"
			" "

			$StatusResult = Invoke-Expression -command 'cmd.exe /c $serviceRestartCmd3'
			$StatusResult2 = $StatusResult
			
			" "
			"----------------------------------"
			"------- Restarting vsanvpd Service -------"
			"----------------------------------"
			" "

			Invoke-Expression -command 'cmd.exe /c $serviceRestartCmd4'
			$row = new-object PSObject -Property @{
				Hostname = $($item.Hostname);
				StatusResult1 = $StatusResult1;
				State1 = 'Complete'
				StatusResult2 = $StatusResult2;
				State2 = 'Complete'
				}
			$table += $row
		
		" "
		"--------------------------------------------------------"
		"-- Finished with Hostname = "+$($item.Hostname)+" --"
		"--------------------------------------------------------"
		" "	


	}

$table | Select-Object Hostname,StatusResult1,StatusResult2,State1,State2 | Export-Csv -Path C:\temp\export.csv -NoTypeInformation