That… Could Be A Problem…

29Oct/130

PowerCLI – Reset NTP settings on VMHosts per Cluster

As daylight savings time is almost upon us, there are always questions revolving around what hosts use what NTP sources for time synchronization. To address this issue, I created a script to go through and remove the current NTP sources, add the desired NTP sources and then restart the NTP service on all of the hosts in the environment. However, to be safe, the preference was that it wasn't done all at the same time. To achieve this, I broke it down and applied it to hosts one by one in a specified cluster.

When running the script, it will ask for the cluster name and four NTP servers so you don't have to modify the code unless you want to add more or less NTP servers.


#Reset NTP settings on VM Hosts per Cluster

$InputCluster = Read-Host "Cluster Name:"
$InputNTP1 = Read-Host "First NTP Server:"
$InputNTP2 = Read-Host "Second NTP Server:"
$InputNTP3 = Read-Host "Third NTP Server:"
$InputNTP4 = Read-Host "Fourth NTP Server:"

#Select Cluster to change NTP Settings
$Cluster = Get-Cluster $InputCluster

#NTP servers to be changed to
$ntp1 = $InputNTP1
$ntp2 = $InputNTP2
$ntp3 = $InputNTP3
$ntp4 = $InputNTP4

#Grabbing VMHosts for desired Cluster
$allVMhost = $Cluster | Get-VMHost | sort Name

#Reseting NTP servers one by one
foreach ($vmhost in $allVMhost){

#Remove existing NTP servers
Write-Host "Removing all NTP Servers from $vmhost"
$allNTPList = Get-VMHostNtpServer -VMHost $vmhost
Remove-VMHostNtpServer -VMHost $vmhost -NtpServer $allNTPList -Confirm:$false | out-null
Write-Host "All NTP Servers from $vmhost have been removed"
Write-Host ""

#Setting NTP servers
Write-Host "Adding NTP servers to $vmhost"
Add-VmHostNtpServer -NtpServer $ntp1,$ntp2,$ntp3,$ntp4 -VMHost $vmhost -Confirm:$false | out-null
Write-Host "The following NTP servers have been added to $vmhost : $ntp1, $ntp2, $ntp3, $ntp4"
Write-Host ""

#Checking NTP Service on the ESXi host
$ntp = Get-VMHostService -vmhost $vmhost| ? {$_.Key -eq 'ntpd'}
Set-VMHostService $ntp -Policy on | out-null

if ($ntp.Running ){
Restart-VMHostService $ntp -confirm:$false
Write-Host "$ntp Service on $vmhost was On and was restarted"
}
Else{
Start-VMHostService $ntp -confirm:$false
Write-Host "$ntp Service on $vmhost was Off and has been started"
}

Write-Host ""

}

Click here to download a text file containing the script.

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.

28Oct/130

PowerCLI – One-Liner – Check All VMHosts’ Time

It's everyone's favorite time of year, daylight savings time. If your NTP solution is solid, there's no worries and you've never had to think twice about it. If you're like everyone else, you at least want to run a couple scripts to verify and validate everything is looking good.

A little bit of background, the environment I was running this report on had 100+ hosts.

An easy one-liner:
Get-VMHost | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}}

Depending on the size of your environment, this could take 20 seconds or an hour. In my case, it took 14 minutes. However, due to the time it took to run, quite a few hosts were reporting different times. A discrepancy of 14 minutes either way meant that the hosts were all just about in sync. If you're only worried about time zone issues, a 14 minute discrepancy is acceptable.

Example based upon the environment I was working in:
PowerCLI C:\ > Get-Date -Format g; Get-VMHost | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}}; Get-Date -Format g

10/9/2013 10:19 AM
Name Current VMHost Time
---- -------------------

10/9/2013 10:33 AM

At this point you may be thinking, there has to be a faster way to do this. You're right, there is. To do so, you have to leave the comfort of the PowerCLI cmdlets behind and make a swap over to the .NET view objects cmdlets. In this case, Get-View will be used.

Good news, it is still another one-liner:
Get-View -ViewType HostSystem -Property Name,ConfigManager.DateTimeSystem | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ConfigManager.DateTimeSystem).QueryDateTime()}}

As you can see, there's a bit of a difference between the one-liners and there will be a bit of a learning curve as well. The time savings will be worth it though. To prove it, this version only took 2 minutes to run. That's a 12 minute savings!

Example based upon the environment I was working in:
PowerCLI C:\ > Get-Date -Format g; Get-View -ViewType HostSystem -Property Name,ConfigManager.DateTimeSystem | sort Name | select Name,@{Name="Current VMHost Time";Expression={(Get-View $_.ConfigManager.DateTimeSystem).QueryDateTime()}}; Get-Date -Format g

10/9/2013 10:35 AM

Name Current VMHost Time
---- -------------------

10/9/2013 10:37 AM

Note: this was a script that worked in my environment. There is no warranty or support with this script, please use at your own risk.