PowerShell Friday: Check time configuration with PowerCLI
Time is important in your network. Almost all components rely on time in one form or another. Authentication for example relies on time stamps to make sure that an authentication token only lives a certain time and is synchronised with the authentication source.
To give your virtual machines and your hosts the correct time you have to make sure that they connect to a reliable time source. I’m not going into which source is best for which occasion, there are a lot of documents and knowledge base articles already written on that subject.
Getting the time for ESXi hosts
Get-VMHost | sort Name | select Name,@{Name=“Current VMHost Time”;Expression={(Get-View $_.ExtensionData.ConfigManager.DateTimeSystem).QueryDateTime()}}
Setting VM time configuration with PowerCLI
The following script I got from DiscoPosse. It configures the VMware Tools so that the VMware Tools sync the time with the ESXi host. I modified it so it only sets the ‘sync time’ checkbox, but not the ‘Upgrade VMware Tools on boot’.
# Connect to your VI Server(s)
Fill in the VI Server(s) using “servername”,”servername” depending on how many you wish to attach to
$VIServers = “vCenterServer1″,”vCenterServer2”
Connect to the VI servers to enumerate the resources
ForEach ($VIServer in $VIServers) { Connect-VIServer $VIServer }
Query for the VM guests
$VMGuests = Get-VM
Loop through your VM guests, set the VM Tools upgrade checkbox and the Sync Time checkbox to true
ForEach ($VMGuest in $VMGuests) { $spec = New-Object VMware.Vim.VirtualMachineConfigSpec $spec.changeVersion = $VMGuest.ExtensionData.Config.ChangeVersion $spec.tools = New-Object VMWare.Vim.ToolsConfigInfo $spec.tools.syncTimeWithHost = $true # Apply the changes $MyVM = Get-View -Id $VMGuest.Id $MyVM.ReconfigVM_Task($spec) }
Host NTP Configuration
@@This is you you configure NTP with PowerCLI.