VMware Cloud Community
Bunty11
Hot Shot
Hot Shot
Jump to solution

Query - NTP service restart

We get list of esxi host which are always not in sync with ntp server by 5 secs.

And the list is aroung 20-25 servers.

Can we create a script which can restart ntp service on all 25 servers.

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I assume that the list of servers is in a CSV file, that looks like this

Hostname

esx1

esx2

...

Then you could do something like this

Import-Csv hostnames.csv -UseCulture | %{
 
Get-VMHost -Name $_.Hostname | Get-VMHostService | where {$_.Key -eq "ntpd"} |
 
Restart-VMHostService -Confirm:$false
}


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

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

I assume that the list of servers is in a CSV file, that looks like this

Hostname

esx1

esx2

...

Then you could do something like this

Import-Csv hostnames.csv -UseCulture | %{
 
Get-VMHost -Name $_.Hostname | Get-VMHostService | where {$_.Key -eq "ntpd"} |
 
Restart-VMHostService -Confirm:$false
}


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

Borja_Mari
Virtuoso
Virtuoso
Jump to solution

Hello,

another option (not using a csv input file):

$allVMhost = Get-VMHost


foreach ($vmhost in $allVMhost){

$ntp = Get-VMHostService -vmhost $vmhost| ? {$_.Key -eq 'ntpd'}
Restart-VMHostService $ntp -confirm:$false
Write-Host "$ntp Service on $vmhost was restarted"
}

Best regards,

Pablo

------------------------------------------------------------------------------------------------- PLEASE CONSIDER AWARDING any HELPFUL or CORRECT reply. Thanks!! Por favor CONSIDERA PREMIAR cualquier respuesta ÚTIL o CORRECTA . ¡¡Muchas gracias!! VCP3, VCP4, VCP5-DCV (VCP550), vExpert 2010, 2014 BLOG: http://communities.vmware.com/blogs/VirtuallyAnITNoob
Bunty11
Hot Shot
Hot Shot
Jump to solution

I guess script from Pablo will restart ntp on all esxi host in my enviorment.

Where as if i only want to restart ntp on 5 esxi host out of 10 esxi host ?

Then we can use LuCD approach. Anyways i wanted to restart ntp on only selected host in my enviorment and not all.

0 Kudos
juriskrumins
Enthusiast
Enthusiast
Jump to solution

Use hybrid approach:

function Restart-VMHostNTPService

{

    [CmdletBinding()]

    [OutputType([int])]

    Param

    (

        # Param1 help description

        [Parameter(Mandatory=$true,

                   ValueFromPipelineByPropertyName=$true,

                   Position=0)]

        [ValidateNotNullOrEmpty()]

        [Alias("Name")]

        [string]$VMHost

    )

    Process

    {

        Get-VMHostService -vmhost $VMHost | ? {$_.Key -eq 'ntpd'} | Restart-VMHostService -confirm:$false

    }

}

Get-VmHost -name host* | Restart-VMHostNTPService

Specify any filter you want using PowerShell capabilities like Where-Object, regular expressions and so on and pipe all this to function.

LucD
Leadership
Leadership
Jump to solution

What is "hybrid" about this ?

The user starts from a list of hostnames which the script needs to read.

If you use a function or not, it is still a Restart-VMHostService you need to do in the end.


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

0 Kudos
Borja_Mari
Virtuoso
Virtuoso
Jump to solution

Hello,

you're right, the LucD's way is what you want Smiley Happy

My script will restart the ntp service in all vmware hosts.

Best regards,

Pablo

------------------------------------------------------------------------------------------------- PLEASE CONSIDER AWARDING any HELPFUL or CORRECT reply. Thanks!! Por favor CONSIDERA PREMIAR cualquier respuesta ÚTIL o CORRECTA . ¡¡Muchas gracias!! VCP3, VCP4, VCP5-DCV (VCP550), vExpert 2010, 2014 BLOG: http://communities.vmware.com/blogs/VirtuallyAnITNoob
0 Kudos
juriskrumins
Enthusiast
Enthusiast
Jump to solution

By "hybrid"  I mean using advanced functions of PowerShell, which is on my mind the best way to do things, cause you can reuse code, load it from your profile, make libraries/modules and so on.

Of course I completely agree with you, that at the end it's Restart-VMHostService, but function handles loops,piping and parameter validation. But those are probably personal preferences.

Anyway all mentioned approaches will work and that's the main goal I believe.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, I see.

And you are of course right, try to create reusable code.

On the other hand in this forum I always try to concentrate on the solution for the question that was asked.

In your answer you assume that the user is at a specific PowerShell version, that he knows about advanced functions, ...

It's not bad, and it should be a best practice for writing PS scripts, but I fear that it might confuse some of the users even more than the simple question they started with.

Just a different point of view :smileycool:


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

0 Kudos