VMware Cloud Community
hogger54
Contributor
Contributor

get / set ESX system time via PowerShell

Hi,

Is there any way to get and set the ESX system time using PowerShell ?

Thanks,

Hogger

0 Kudos
8 Replies
Chamon
Commander
Commander

get-date After you have connected to the host

Ok so that doesn't work. You can set the NTP server with set-vmhostntpserver

Message was edited by: Chamon

0 Kudos
LucD
Leadership
Leadership

The Get-Date cmdlet will give you the time on the machine where you execute the cmdlet.

To get the time from an ESX server you can do the following

$esxName = <ESX-name>
Connect-VIServer -Server $esxName -Credential (Get-Credential)

(Get-View ServiceInstance).CurrentTime()

Disconnect-VIServer -Confirm:$false

Note that you have to connect to the ESX server for this method.

If you execute the method against the vCenter you will get the time from the vCenter server.

____________

Blog: LucD notes

Twitter: lucd22


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

Chamon
Commander
Commander

Can you set the time? Wu use an NTP server so we don't set the time on the host after the instal just the NTP servers. Trying to learn PS. Next lesson is about Objects so hopefully that will help with this sort of thing. Any advise on training material?

0 Kudos
LucD
Leadership
Leadership

Yes, you could use the UpdateDateTime method.

You could have a look at some of my blog posts. See my signature.

I have a few on the SDK and I have the My PS library post which contains some links to good material.

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Chamon
Commander
Commander

Great Thanks

0 Kudos
jeveenj
Enthusiast
Enthusiast

Hi,

You can get\set date and time of ESX using below code snippet

$creds = Get-VICredentialStoreItem -file C:\vicredentials.xml
Connect-VIServer -Server $creds.Host -user $creds.User -password $creds.Password
#For current time 
(Get-View ServiceInstance).CurrentTime()
$vmhost = Get-vmhost $creds.Host |Get-View
$dtsystemMoRef = $vmhost.ConfigManager.DateTimeSystem
$dtsystem = Get-View $dtsystemMoRef
#For changing datetime, change the $dateconfig
$dateConfig = "Thursday, February 04, 2010 6:55:18 AM"
$dtsystem.UpdateDateTime($dateConfig)

Hope this helps

-If you found this information useful, please consider awarding points for Correct or Helpful.
hogger54
Contributor
Contributor

Thanks for your excellent reply !!!!

Worked like a champ.

0 Kudos
Dennisch
Contributor
Contributor

Thanks! Works like a charme!

0 Kudos