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.
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
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
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
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.
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.
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
Hello,
you're right, the LucD's way is what you want
My script will restart the ntp service in all vmware hosts.
Best regards,
Pablo
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.
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