0

I'm looking for an easy way to get the date and time of an esx server in c#. This has to be the hardware time, so I think this is the BIOS time. When a VM reboots it normally gets the time of the BIOS time. It is that time I want as a datetime object.

I think I should use something like this:

private DateTime getTimeESX(Machine machine)
{
    DateTime time = new DateTime();

    Runspace runSpace = RunspaceFactory.CreateRunspace();
    runSpace.Open();
    Pipeline pipeline = runSpace.CreatePipeline();

    Command getEsxCli = new Command("Get-EsxCli");
    getEsxCli.Parameters.Add("Server", machine.IP);
    Command getSysTime = new Command("system time get");
    pipeline.Commands.Add(getEsxCli);
    pipeline.Commands.Add(getSysTime);

    Collection<PSObject> output = pipeline.Invoke();
    foreach (PSObject psObject in output)
    {
        time = (DateTime)psObject.BaseObject;
    }
    return time;
}

Does someone has an idea how I could easily test this? I don't have an ESX running in my network.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Please elaborate. Are you reaching out over the network to an ESX server? Or are you wanting a hardware time of the host OS from code running in a VM? Or something else? If you just want the machine time of the code you're running on, then ESX has nothing to do with it. Please also provide any code samples of what you tried that didn't work, or what you might expect to work - even if it doesn't exist in the SDK. That will help clarify your question. – Matt Johnson-Pint May 21 '13 at 18:56
  • The idea is to monitor and log the timestamps of all crucial devices in the network such as workstations, ntp server, esx and others. I'm well aware of the fact that i can aproach each VM seperatly to get the timestamp. But I want the timestamp of the esx hardware machine itself. Until now I only have the basic of the method: – Kenny Batselier May 21 '13 at 19:20
  • using VMware.Vim; private DateTime getTimeESX(string ip, string user, string pass) { DateTime time = new DateTime(); VimClient c = new VimClient(); ServiceContent sc = c.Connect(ip); UserSession us = c.Login(user, pass); return time; } With this code I should be able to connect to the esx but I do not know what commands to use to receive a timestamp.. thx, Kenny – Kenny Batselier May 21 '13 at 19:21

1 Answers1

1

Since the docs show everything as Powershell cmdlets, you will have to translate this to C# yourself. But you can do the following:

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575