Thursday, January 17, 2013

Powershell VMware NTP


Purpose:
This is my first powershell script. It connects to a esxi host and removes all NTP servers dynamically then sets the new ones.

param([string]$VC, [string]$NTP1, [string]$NTP2)

If ($VC){$strVC = $VC}
ELSE{$strVC = Read-Host "What is the Vcenter hostname?"}
If (!$strVC){Write-Host "Error: Vcenter not entered";exit}

If ($NTP1){$strNTP1 = $NTP1}
ELSE{$strNTP1 = Read-Host "What is the first NTP server?"}
If (!$strNTP1){Write-Host "Error: NTP1 not entered need at least one NTP server";exit}

If ($NTP2){$strNTP2 = $NTP2}
ELSE{$strNTP2 = Read-Host "What is the second NTP server?"}


Connect-VIServer -Server $strVC  #Enter your vCenter Server

$NtpServers = @($strNTP1,$strNTP2)
#http://day2dayadmin.blogspot.com/2009/10/ntp-powershell-its-about-time.html
$ESXHosts = Get-VMHost | Select-Object Name,@{Name="NTP Server";Expression={$_ | Get-VMHostNtpServer}}, @{Name="NTP Running";Expression={($_ | Get-VMHostService | Where-Object {$_.key -eq "ntpd"}).Running}} | Sort-Object -Property "NTP Running", "NTP Server"



ForEach ($ESXHost in $ESXHosts) {
   
   
#Write-Host $ESXHost

   #remove current NTP servers
   Write-Host "Removing NTP servers" -BackgroundColor "Green" -ForegroundColor "Black"
   Remove-VMHostNtpServer -NtpServer $ESXHost."NTP Server" -VMHost $ESXHost.Name
   #Add new NTP servers
   Write-Host "Adding New NTP servers" -BackgroundColor "Green" -ForegroundColor "Black"
   Add-VmHostNtpServer -NtpServer $NtpServers -VMHost $ESXHost.Name
   #Stop NTP service   
   Write-Host "Stopping NTP Service" -BackgroundColor "Green" -ForegroundColor "Black"
   Get-VmHostService -VMHost $ESXHost.Name | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService
 
}
Write-Host "Done!"

1 comment:

  1. The casino provides sports betting markets, casino video games, E-sports, live casinos, financials, forex, video games, lottery, and virtual sports. Even though totally different web sites claim to supply reliable support for you, want to} at all 카지노 사이트 times conscious of|concentrate on|pay attention to} on-line playing. Because the very fact fact} that|although} it is a medium of leisure, this entails your hard-earned money. You should not just open a portal and begin playing in} because of|as a outcome of} someone mentioned it to be a great one. Big worldwide playing websites are actual and so they have numerous verified members and evaluations. But it’s also good to play in your local on line casino websites as they are your personal people and who is aware of}, you would possibly find it to be more pleasant like being at house.

    ReplyDelete