Skip to main content

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!"

Comments

Popular posts from this blog

Excel document for pinging list of computers VBA

Here is an excel document I created that will ping a list of nodes in column A and give results in column B. There are much better tools that can be used such as angry ip scanner  http://www.angryip.org/w/Home . I just wrote this as an example Requirements: Tested with Office 2010 Download: Download

Powershell - Com+ Application Recycle

Needed a script to recycle a com+ application nightly and this is what i came up with. This script will write each recycle it does to the event log under application. Run locally or via a scheduled task. #Recycle COM+ Application and write to the event log the status # 1.0 Release # Run script locally # Write to the event log ######################################## #Configurable ######################################## #Com+ ApplicationName $ComPlusLikeAppName = "Put the name of Com+ Application here a like statement is used to eval so you can get away with putting part of it" #EventLog to write to. $eventlog = "Application" #Source for eventlog. $source = "RecycleComObject" #Successful Event ID $SEventID = 0 #Error Event ID $EEventID = 666 #Process that COM+ runs under $process = "dllhost.exe". ######################################## #Clear $CurrentMemory = $null $PRocessID = $null $Commandline = $null $GUID = $null $AppID = $null $Messag

Powershell : Certutil Find Expired Certs on CA server

Wrote this to get certificate expiration information for certificates that expired 5 days ago to ones that expire in 90 days. Wrap an invoke-command around this for remote query. $Before = (get-date).adddays(90).ToString("MM/dd/yyyy") $After = (get-date).AddDays(-5).ToString("MM/dd/yyyy") <# https://blogs.technet.microsoft.com/poshchap/2016/01/01/powershell-and-certutil-exe/ We create a date range with $Before, i.e. certificates expiring before this date, and $After, i.e. certificates expiring after this date. These values are converted into something that certutil can understand - $Restrict. This is then used with the certutil -restrict parameter. #> $Restrict = "NotAfter<=$Before,NotAfter>=$After" $Report = @() $cmd = & certutil.exe -view -restrict $Restrict -out "RequesterName,CommonName,Certificate Expiration Date","Certificate Template" $SplitLines = $cmd.Split("`n`r") $Index = 0 foreach ($line in $Sp