VMware Cloud Community
CG21
Enthusiast
Enthusiast
Jump to solution

plan a task to restart a service like ntpd on all esx in vcenter

Hi,

i think the title is preaty clear. We have a vcenter versin 6.7 with multiple host on esxi server 6.7.0.

i would like to plan a task to restart a service like ntp on all esx in this vcenter,

i have this script, is it ggod for you ?

Connect-viserver vcenter1 -user vcenter -pass password

foreach($esx in Get-VMHost){   

Restart-VMHostService -HostService ( $_ | Get-VMHostService | Where { $_.Key -eq "ntpd" } )

}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Watch out with what is on the pipeline.
Better would be a full pipeline construct. Something like this

Connect-viserver vcenter1 -user vcenter -pass password

Get-VMHost |

Get-VMHostService | where{$_.Key -eq 'ntpd'} |

Restart-VMHostService -Confirm:$false


But what do you mean by "plan a task to restart"?

Would this be a scheduled task?


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

View solution in original post

Reply
0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Watch out with what is on the pipeline.
Better would be a full pipeline construct. Something like this

Connect-viserver vcenter1 -user vcenter -pass password

Get-VMHost |

Get-VMHostService | where{$_.Key -eq 'ntpd'} |

Restart-VMHostService -Confirm:$false


But what do you mean by "plan a task to restart"?

Would this be a scheduled task?


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

Reply
0 Kudos
sarikrizvi
Enthusiast
Enthusiast
Jump to solution

1. To check status of NTP services on all Hosts :-

Get-VMHost | Select Name, @{N="NTPServer";E={$_ | Get-VMHostNtpServer}}, @{N="ServiceRunning";E={(Get-VmHostService -VMH

ost $_ | Where-Object {$_.key -eq "ntpd"}).Running}}

2.To Configure NTP Server, Config Policy & Start it, run below:-

Get-VMHost | Add-VMHostNtpServer PUTNTPServerIPs

Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true

Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService

Get-VMhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"

Regards,
SARIK (Infrastructure Architect)
vExpert 2018-2020 | vExpert - Pro | NSX | Security
vCAP-DCD 6.5 | vCP-DCV 5.0 | 5.5 | 6.0 | vCA-DCV 5 | vCA-Cloud 5 | RHCSA & RHCE 6 | A+ (HW & NW)
__________________
Please Mark "Helpful" or "Correct" if It'll help you
_____________________________________
@Follow:
Blog# https://vmwarevtech.com
vExpert# https://vexpert.vmware.com/directory/1997
Badge# https://www.youracclaim.com/users/sarik
Reply
0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

from time to time some of the vcenter's esx do not have their clock up to date, so the vms too. simply restarting the service resets the clock. it usually happens at every time change (summer / winter)

so i would have a scheduled task to plan this script.

thank you for your answers . i'm going to try it

Reply
0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

Hi , itry this script but i have this error

pastedImage_0.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you are missing the 1st line (Get-VMHost |) or at least the pipeline symbol at the end of that line


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

Reply
0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

Hi,

its not missing the pipeline look, when i just run all in one line i have the same error,

and when i run just the line Get-VMHost with the pipeline it's missing anythink

look the attached screen

than you for your help

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

My bad, I had a copy/paste error.
I corrected the code above, please try again.


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

Reply
0 Kudos
CG21
Enthusiast
Enthusiast
Jump to solution

Great, Thank you so much for your help.

it's ok.

Reply
0 Kudos