How to get the current date/time stamp from a network computer?

  • Thread starter Thread starter trytobreak
  • Start date Start date
T

trytobreak

Hi All,

I am a network administrator in a fairly large software company and I
would like to write myself a small utility, which would connect (one
by one) to all machines on the network and get their current date and
time stamps.

This is mostly because of coming DST changes and having hundreds of
machines on the network, I don't want to connect remotely to every and
each server to see if the time is correct.

Is there any function or command in VB.NET which lets me grab a
current date/time from a network computer?

Please let me know, you could save me a lot of time :)

Thanks,
JK
 
Hi All,

I am a network administrator in a fairly large software company and I
would like to write myself a small utility, which would connect (one
by one) to all machines on the network and get their current date and
time stamps.

This is mostly because of coming DST changes and having hundreds of
machines on the network, I don't want to connect remotely to every and
each server to see if the time is correct.

Is there any function or command in VB.NET which lets me grab a
current date/time from a network computer?

Please let me know, you could save me a lot of time :)

Thanks,
JK

Well... Assuming NT based machines you could always automate the NET
TIME command.
 
Funnily I said something along the same lines on the c# group :)

----- Original Message -----
From: "Brendon Bezuidenhout" <[email protected]>
Newsgroups: microsoft.public.dotnet.languages.csharp
Sent: Monday, October 29, 2007 4:00 PM
Subject: Re: How to get the current date/time stamp from a network computer?

Hey,

No offence but if you are the network admin would it not be easier to send
a
batch down on next login and force the Internet Time to reset to the most
current "real time"? I admit I might be going off at a tangent slightly
but
it could do the job and save you tons of time having to write a tiny app
to
do the work windows can on it's own - IF you tell it *grin*...

Something like this in a .bat file
<code>
net stop w32time
w32tm /unregister [ignore error message]
w32tm /unregister [enter a second time]
w32tm /register
reg add hklm\system\currentcontrolset\services\w32time\parameters\ /v
NtpServer /t reg_sz /d time.nist.gov /f
net start w32time
</code>

I think there is/was an error with time.windows.com or another of the time
prefix ones as they don't/won't accept requests in TIME format anymore -
nist.gov will cause no issues.

HTH
Brendon
 
Hi All,

I am a network administrator in a fairly large software company and I
would like to write myself a small utility, which would connect (one
by one) to all machines on the network and get their current date and
time stamps.

This is mostly because of coming DST changes and having hundreds of
machines on the network, I don't want to connect remotely to every and
each server to see if the time is correct.

Is there any function or command in VB.NET which lets me grab a
current date/time from a network computer?

Please let me know, you could save me a lot of time :)

Thanks,
JK

Reply, in case you want to use the API for this, you can use
NetRemoteTOD.
 
Hi,

I was really looking for an answer to my problem...


Problem is, you see, that we are not syncing (for various reasons)
with time servers.
So I need to know if the time is correct some other way and this would
be the best.


Anyhow, is someone here capable to answer the question? :)
Can I remotely get the network computer's time and date?

And if so, could someone tell me how?
 
Sorry, I have overlooked...
I am researching now the NetRemoteTOD. Quite interesting.
Thanks, lets see if I can get it to work.
 
Sorry, I have overlooked...
I am researching now the NetRemoteTOD. Quite interesting.
Thanks, lets see if I can get it to work.

Here is some c# code, I wrote to try out this function. You could
convert this to vb, and modify it to loop through a list of machines
- either from a file, or enumerating the machines on the network.

class Program
{
static void Main ( string[] args )
{
IntPtr handle = IntPtr.Zero;
if (NetRemoteTOD ( "\\\\yournetworkpc", ref handle ) ==
NERR_Success)
{
TIME_OF_DAY_INFO time =
(TIME_OF_DAY_INFO)Marshal.PtrToStructure ( handle, typeof
( TIME_OF_DAY_INFO ) );

// new date time. The hours are in utc, so you hae to
use the timezone
// offset.
DateTime dt = new DateTime ( (int)time.tod_year,
(int)time.tod_month, (int)time.tod_day, (int)(time.tod_hours -
(time.tod_timezone / 60)), (int)time.tod_mins, (int)time.tod_secs );

Console.WriteLine ( dt );

uint result = NetApiBufferFree ( handle );
if (result != NERR_Success)
Console.WriteLine ( "Memory cleanup failed: {0}",
result );
}
}

private const uint NERR_Success = 0;

/// <summary>
/// Free the buffer allocated by the NetRemoteTOD function
/// </summary>
/// <param name="Buffer">pointer to the buffer</param>
/// <returns>NERR_Success on success, else the system error
code.</returns>
[DllImport("netapi32.dll", SetLastError=false)]
private static extern uint NetApiBufferFree ( IntPtr Buffer );

[DllImport ( "netapi32.dll", CharSet = CharSet.Unicode,
SetLastError = false )]
private static extern uint NetRemoteTOD ( string
UncServerName, ref IntPtr BufferPtr );

[StructLayout(LayoutKind.Sequential)]
private struct TIME_OF_DAY_INFO
{
public uint tod_elapsedt;
public uint tod_msecs;
public uint tod_hours;
public uint tod_mins;
public uint tod_secs;
public uint tod_hunds;
public uint tod_timezone;
public uint tod_tinterval;
public uint tod_day;
public uint tod_month;
public uint tod_year;
public uint tod_weekday;
}
}
 
Sorry, I have overlooked...
I am researching now the NetRemoteTOD. Quite interesting.
Thanks, lets see if I can get it to work.

Here is some c# code, I wrote to try out this function. You could
convert this to vb, and modify it to loop through a list of machines
- either from a file, or enumerating the machines on the network.

class Program
{
static void Main ( string[] args )
{
IntPtr handle = IntPtr.Zero;
if (NetRemoteTOD ( "\\\\yournetworkpc", ref handle ) ==
NERR_Success)
{
TIME_OF_DAY_INFO time =
(TIME_OF_DAY_INFO)Marshal.PtrToStructure ( handle, typeof
( TIME_OF_DAY_INFO ) );

// new date time. The hours are in utc, so you hae to
use the timezone
// offset.
DateTime dt = new DateTime ( (int)time.tod_year,
(int)time.tod_month, (int)time.tod_day, (int)(time.tod_hours -
(time.tod_timezone / 60)), (int)time.tod_mins, (int)time.tod_secs );

Console.WriteLine ( dt );

uint result = NetApiBufferFree ( handle );
if (result != NERR_Success)
Console.WriteLine ( "Memory cleanup failed: {0}",
result );
}
}

private const uint NERR_Success = 0;

/// <summary>
/// Free the buffer allocated by the NetRemoteTOD function
/// </summary>
/// <param name="Buffer">pointer to the buffer</param>
/// <returns>NERR_Success on success, else the system error
code.</returns>
[DllImport("netapi32.dll", SetLastError=false)]
private static extern uint NetApiBufferFree ( IntPtr Buffer );

[DllImport ( "netapi32.dll", CharSet = CharSet.Unicode,
SetLastError = false )]
private static extern uint NetRemoteTOD ( string
UncServerName, ref IntPtr BufferPtr );

[StructLayout(LayoutKind.Sequential)]
private struct TIME_OF_DAY_INFO
{
public uint tod_elapsedt;
public uint tod_msecs;
public uint tod_hours;
public uint tod_mins;
public uint tod_secs;
public uint tod_hunds;
public uint tod_timezone;

In retro-spect, the tod_timezone member should be int, not uint. It
returns a negative value for timezones east of the Prime Meridian.
 
Back
Top