VB.net 2005 getting the system time of the remote computer?

I am trying to set up an "AT" job to export some registry keys on a remote computer, the problem is that the DOS command takes a while to start. I want to get the system time of the remote computer, so I can schedule it to start 1 minute after the command was sent.

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

+1


a source to share


3 answers


here is what i have to work, thanks for 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)

      

+1


a source


You can use:

net time \\computer_name

      

You just need to lay it out and analyze the result.




Here's some sample code. It's in C #, but needs to be translated easily.

    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 error handling (if no machine is found, etc.) - you can add whatever you need to suit your goals.

0


a source


.. and maby do a little check if you are using

    ...
    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);
    }

      

0


a source







All Articles