VMware Cloud Community
MaydayIT
Contributor
Contributor
Jump to solution

Reset NTP Service on all hosts

Hi all.

I've been in IT for almost thirty years, and people are always telling me I should learn "language X."  They tell me it will save me time and aggrivation.  Of course, the latest is Powershell, and since I'm woriking in tVMWare infrastructure, PowerCLI just kind of follows along.  So here I sit, looking at what should be a relatively simple script, spinning my wheels wasting more ime than it would take to just do it manually in the gui!  So here I sit feeling my blood pressure rise and wonder why I bother trying this crap!  Sorry for ranting a bit, but maybe one day someone will invent a anguage that truly SIMPLIFIES THINGS!

My task was simple.  Change the NTP server entries on our VMWare hosts.  So I found a script in one of the forumns, and although it had "logic" (yeah right!) to restart the service after making the change, it didn't do that, so this morning, I went looking for some code to do just that last piece.  I found a script on Simon Long's blog that looked simple enough, and I could follow the logic of the commands.  So, I pasted it into a script and edited it for my environment.

Here's the script.

$Hosts = Get-VMHost

ForEach ($Host in $Hosts)

{

Remove-VmHostNtpServer -NtpServer "<old ntp server>" -VMHost $Host | Out-Null

Add-VmHostNtpServer -NtpServer "<new ntp server>" -VMHost $Host | Out-Null

Get-VmHostService -VMHost $Host | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | Out-Null

write "NTP Server was changed on $Host"

}

Since I had already changed the host to the new address, I deleted the first two lines in the loop.  I started it off with a connect-viserver command, saved it, ran it and it bombed on the foreach loop stating that $host was read-only and couldn't be changed.  I DIDN'T ASK IT TO CHANGE IT!!!!!!  So I'm about ready to rename PowerCLI to yet another UselessPOS!  Can anyone give me som REAL logic as to why it thinks it needs to change this variable?????????????

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Your script is all right, just change the name of the loop variable.

Something like this for example

$Hosts= Get-VMHost

ForEach($esx in $Hosts)

{

   Remove-VmHostNtpServer-NtpServer"<old ntp server>"-VMHost $esx| Out-Null

   Add-VmHostNtpServer-NtpServer"<new ntp server>"-VMHost $esx| Out-Null

   Get-VmHostService-VMHost $esx| Where-Object{$_.key -eq"ntpd"} | Restart-VMHostService-Confirm:$false| Out-Null

   write"NTP Server was changed on $esx"

}


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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

There are a couple of "reserved" variables in PowerShell and $host happens to be one of them I'm afraid.

Pick another name for the variable and you should be good.


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

0 Kudos
MaydayIT
Contributor
Contributor
Jump to solution

Ok, I figured that $host was a reserved variable, because it populates by itself.  However, I still can't figure out why looping through the entries requires writing the variable.  Where in the above code have I told it to write to the variable??  I told it to write to $hosts, but not to $host.  It still seems to me that every time they add new bells and whistles, they forget to keep the logic.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid you did in the ForEach loop

ForEach($Host in $Hosts)

The $Host variable will on each loop be assigned one of the values from $Hosts.


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

MaydayIT
Contributor
Contributor
Jump to solution

Ok, so after setting a variable to contain the hosts on a given server, how do I loop through the collection it creates and simply use the data in a command within the loop?  I wish I had more of a head for this stuff.  Thanks for your patience Smiley Happy.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Your script is all right, just change the name of the loop variable.

Something like this for example

$Hosts= Get-VMHost

ForEach($esx in $Hosts)

{

   Remove-VmHostNtpServer-NtpServer"<old ntp server>"-VMHost $esx| Out-Null

   Add-VmHostNtpServer-NtpServer"<new ntp server>"-VMHost $esx| Out-Null

   Get-VmHostService-VMHost $esx| Where-Object{$_.key -eq"ntpd"} | Restart-VMHostService-Confirm:$false| Out-Null

   write"NTP Server was changed on $esx"

}


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

0 Kudos
MaydayIT
Contributor
Contributor
Jump to solution

Thans LucD!

Initially, I thought that the $host variable would just be READ by the system as it looped through the hosts.  When you suggested changing the variable, I thought I'd need to add some more code to ppulate the host name into the new variable.  Thanks for setting me straight.

0 Kudos