Click here to Skip to main content
Click here to Skip to main content
Go to top

Fetch reboot time of remote servers

, 6 Sep 2012
Rate this:
Please Sign up or sign in to vote.
Test whether your remote server rebooted or not from C# code.

Introduction

This article explains how to test from C# code whether your remote server or personal computer rebooted or not and get the last reboot time. Without logging into a machine we can get the last reboot time of machine from C# code.

Background

I am supporting some remote servers and patches will be installed on these regularly. To maintain the avalability of these servers I need to check the last reboot time, and for this I need to log on to the server manually to get the reboot time by running the below command on CMD.

SYSTEMINFO | FIND " Up Time"

This takes a lot of time, to log on to the machine and get this.

Using the code

The System.Management class provides us sytem information of remote machines by creating an object as follows.

After creating the ManagementScope object, use this and create a ProcessClass object and call the GetInstances() function, and get the Last Reboot Time property of the machine. Like this we can get the processor speed and hardware capacity.

ConnectionOptions connOptions = new ConnectionOptions();
connOptions.Username = userName;
connOptions.Password = password;
connOptions.Impersonation = ImpersonationLevel.Impersonate;
connOptions.EnablePrivileges = true;
ManagementScope manScope;

try
{
  manScope= new ManagementScope(string.Format(@"\\{0}\ROOT\CIMV2", 
                serverName), connOptions);
  manScope.Connect();
}

ObjectGetOptions objectGetOptions = new ObjectGetOptions();
ManagementPath managementPath = new ManagementPath("Win32_OperatingSystem");
ManagementClass processClass = new ManagementClass(manScope, 
                managementPath, objectGetOptions);

foreach (ManagementObject item in processClass.GetInstances())
{
    datetime = item["LastBootUpTime"].ToString();
}

Points of Interest

From this I learnt how to access machine information from C# using the class System.Management.ManagementScope. I am now preparing a UI thing for easy usage of the code.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by Suraj S Koneri
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid