views:

755

answers:

3

I am trying to set up an "AT" job to export some registry keys on a remote computer, the problem is that DOS command requires a time to run. I want to get the system time of the remote computer so i can schedule it to run 1 minute from the time i send the command.

Is there any way to get the system time of a remote computer with VB.Net code?

Thanks

A: 

You can use:

net time \\computer_name

You'll just need to shell out to this and parse the result.


Here's some sample code. It's in C#, but should be pretty easy to translate.

    static DateTime GetRemoteDateTime(string machineName)
    {
        machineName = @"\\" + machineName;
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo("net", "time " + machineName);
        startInfo.RedirectStandardOutput = true;
        startInfo.UseShellExecute = false;
        System.Diagnostics.Process p = System.Diagnostics.Process.Start(startInfo);
        p.WaitForExit();

        string output = p.StandardOutput.ReadLine();
        output = output.Replace("Current time at " + machineName + " is ", "");
        return DateTime.Parse(output);
    }

I didn't bother adding any error handling (if the machine is not found, etc) - you can add in whatever you need to suit your purposes.

Jon B
Thanks!Can you give me an example of how to parse the result, not sure how to do that.
Edited to include sample code
Jon B
getting and error with StandardOutput.Readline StandardOut has not been redirected or the process hasn't started yet.any idea what that means? will post my code if you need to see it.thanks again for your help!
Works for me... Setting RedirectStandardOutput to True is critical.
Jon B
+1  A: 

here is what i got to work, thanks for all your help Jon B.

    Dim p As New System.Diagnostics.Process
    Dim pinfo As New System.Diagnostics.ProcessStartInfo
    Dim pout As String
    pinfo.FileName = ("C:\WINDOWS\system32\net.exe")
    pinfo.Arguments = ("time \\computername")
    pinfo.RedirectStandardOutput = True
    pinfo.UseShellExecute = False
    pinfo.CreateNoWindow = True
    p = Diagnostics.Process.Start(pinfo)
    p.WaitForExit()
    pout = p.StandardOutput.ReadLine
    MsgBox(pout)
A: 

..and maby do a little check if you use

    ...
    startInfo.RedirectStandardOutput = true;
    startInfo.RedirectStandardError = true;
    ...

    string shellOut = "";
    if (p.ExitCode == 0)
    {
        shellOut = p.StandardOutput.ReadToEnd();
        Console.WriteLine("Operation completed successfully.");
    }
    else
    {
        shellOut = p.StandardError.ReadToEnd();
        Console.WriteLine(shellOut);
    }
anders dahl