NYSDHCR
Enthusiast
Enthusiast

Script to change DNS and NTP needed

I am new to scripting. I am looking for a script that will change the two DNS server IPs as well as change the NTP server and restart the NTP servicen of an ESX host. Could it also make the change on an esx host as well as a datacenter level?

Thanks!

0 Kudos
5 Replies
LucD
Leadership
Leadership

To change the DNS servers you can use the Set-VMHostNetwork cmdlet.

Get-VMHost -Name MyEsx | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress "192.168.1.1","192.168.1.2"

To change the NTP server use the Add-VMHostNtpServer cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
NYSDHCR
Enthusiast
Enthusiast

what would the script be to pit it all together in one file?

0 Kudos
LucD
Leadership
Leadership

You mean something like this

$esxName = "MyESX" 
$dnsSrv
= "192.168.1.1","192.168.1.2"
$ntpSrv
= "192.168.1.3"
$esx = Get-VMHost -Name $esxName
Get-VMHostNetwork
-VMHost $esx | Set-VMHostNetwork -DnsAddress "192.168.1.1","192.168.1.2"
Remove-VMHostNtpServer -VMHost $esx -NtpServer (Get-VMHostNtpServer -VMHost $esx) -Confirm:$false
Add-VmHostNtpServer
-VMHost $esx -NtpServer $ntpSrv

Note that this will remove the current NTP server and replace it by the one(s) defined in $ntpSrv


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
65cat
Contributor
Contributor

I am also very new to scripting and looking for something very similar. I need to query my hosts for incorrect dns and ntp settings and change them to the correct settings.  Thank you for your time.

0 Kudos
LucD
Leadership
Leadership

Why the testing, unless you need reporting on that?
Why not just set the values?

Like this

$dnsSrv = "192.168.1.1","192.168.1.2"

$ntpSrv = "192.168.1.3"

foreach($esx in Get-VMHost){

    Get-VMHostNetwork -VMHost $esx | Set-VMHostNetwork -DnsAddress $dnsSrv

   

    Remove-VMHostNtpServer -VMHost $esx -NtpServer (Get-VMHostNtpServer -VMHost $esx) -Confirm:$false

    Add-VmHostNtpServer -VMHost $esx -NtpServer $ntpSrv

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos