Tuesday, August 30, 2016

Configuring NTP server using PowerCLI

Below scripts executes against all the ESXi hosts under the vCenter Server. 

1) Remove old NTP server settings & add new NTP server settings:

connect-viserver vcenterservername
$oldntpservers='192.168.1.254'
$newntpservers='192.168.2.254'
foreach($vmhost in get-vmhost){
#stop ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Stop-VMHostService -Confirm:$false
#remove ntpservers 
$vmhost|Remove-VMHostNtpServer -NtpServer $oldntpservers -Confirm:$false
#add new ntpservers
$vmhost|Add-VmHostNtpServer -NtpServer $newntpservers
#start ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Start-VMHostService
}

2) Add new NTP server settings:

Connect-VIServer vcenterservername
Get-VMHost | Add-VMHostNtpServer 192.168.1.253
Get-VMHost | Add-VMHostNtpServer 192.168.1.254
Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true
Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService

Get-VMhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"

No comments:

Post a Comment