JDMils_Interact
Enthusiast
Enthusiast

PowerCLI script to set multiple NTP servers per VMhost

I found two PowerCLI scripts here:

https://captainvops.com/2020/07/17/get-and-set-ntp-settings-of-all-vmware-hosts-with-powercli/

The first one checks the state of the hosts' NTP settings/services. The second script sets the NTP servers and activates the NPT service. However the original script only works with one NTP server and we always have 2 NTP servers on our hosts. So I modified the script to allow for 2 NTP servers per host, and it seems to work, however I'm not sure if this is the most efficient way to do this.

The original script which sets the one NTP server and activates the NTP service:

 

Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false; Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on"; Get-VMHost | Add-VMHostNtpServer -NtpServer pool.ntp.org; Get-VMHost | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true; Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService

 

My modified version:

 

Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Stop-VMHostService -Confirm:$false; Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "on"; Get-VMHost | Add-VMHostNtpServer -NtpServer 172.10.10.51; Get-VMHost | Add-VMHostNtpServer -NtpServer 172.10.10.52; Get-VMHost | Get-VMHostFirewallException | Where-Object {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true; Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService

 

My concern is that there are a lot of "Get-VMHost" commands which may slow down the script, maybe with one-liners this is how it's done, but I much prefer using structured code with loops so I'm going to modify the code so that it only gets the required properties once, and setting the NTP servers this way should not be a problem. But I still would like to know if the modified script is the way to go with the one-liner version?

 

 

Tags (1)
0 Kudos
0 Replies