PowerCLI

 View Only
  • 1.  Query - NTP service restart

    Posted Jun 26, 2014 08:04 AM

    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.



  • 2.  RE: Query - NTP service restart
    Best Answer

    Posted Jun 26, 2014 09:07 AM

    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
    }


  • 3.  RE: Query - NTP service restart

    Posted Jun 26, 2014 09:17 AM

    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



  • 4.  RE: Query - NTP service restart

    Posted Jun 26, 2014 11:09 AM

    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.



  • 5.  RE: Query - NTP service restart

    Posted Jun 26, 2014 11:25 AM

    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.



  • 6.  RE: Query - NTP service restart

    Posted Jun 26, 2014 11:30 AM

    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.



  • 7.  RE: Query - NTP service restart

    Posted Jun 26, 2014 11:45 AM

    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.



  • 8.  RE: Query - NTP service restart

    Posted Jun 26, 2014 11:52 AM

    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:



  • 9.  RE: Query - NTP service restart

    Posted Jun 26, 2014 11:36 AM

    Hello,

    you're right, the LucD's way is what you want :smileyhappy:

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

    Best regards,

    Pablo