ESXi using PowerCLI to set the time and batch configuration NTP

In the modern enterprise business system or production, time synchronization is important. Now VMware virtualized applications is very broad, ESXi host to a unified accurate time, but also to the client system an accurate time.

There are very few host environment can be set manually, but several to 10 or even more, slow and error-prone manual settings. Well, I believe you are lazy, lazy lazy way there.

About VMware PowerCLI what it is, and how to install, the  command-line operation vSphere-VMware PowerCLI installation  this article are described in detail, it is no longer complaining here.

Well PowerCLI principle is to use VC connected to the top go to the next issue ESXi configuration and configure the restart ntp, ESXi all the advanced settings can be set up, I found that really a good thing!

Well, here's direct throw out the code. Here's a one-time set-up time and the NTP in two ways, what they need. Of course, all fully automated.

Connection VC #
$ VC = '192.168.xx'
$UName='[email protected]'
$ UPass = your pass'
Connect-VIServer -Server $VC -User $UName -Password $UPass

# Change the host time
Foreach($HostIP in Get-VMHost){
	$esxcli = Get-EsxCli -VMHost $HostIP -V2
	$arguments = $esxcli.system.time.set.CreateArgs()
	$arguments.year = $(Get-Date -Format 'yyyy')
	$arguments.month = $(Get-Date -Format 'MM')
	$arguments.day = $(Get-Date -Format 'dd')
	$arguments.hour = $(Get-Date -Format 'HH')-8
	$arguments.min = $(Get-Date -Format 'mm')
	$esxcli.system.time.set.Invoke($arguments)
}
# Set the NTP
Foreach($HostIP in Get-VMHost){
	Add-VmHostNtpServer -NtpServer "120.25.115.20" -VMHost $HostIP
	Add-VmHostNtpServer -NtpServer "ntp1.aliyun.com" -VMHost $HostIP
	Get-VMHostService -VMHost $HostIP | where { $_.Key -eq "ntpd" } | Start-VMHostService
	Get-VMHostService -VMHost $HostIP | where { $_.Key -eq "ntpd" } | Set-VMHostService -Policy "on" -Confirm:$false
}
# Query host time
Foreach($HostIP in Get-VMHost){
	$esxcli = Get-EsxCli -VMHost $HostIP -V2
	$esxcli.system.time.get.Invoke()
}

  

Get-VMHost get that all the current hosts, the role of VC category is currently below the bottom of all hosts VC.

Interpretation: The first code block is connected to VC, the second is to get the current time you set the machine to ESXi hosts, and the third is to set the NTP and restart the service and set the boot from Kai, and the fourth is a query at each host time. Know something about programming or wise junior partner at a glance understand!

Guess you like

Origin www.cnblogs.com/zilong666/p/11032564.html