Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Get uptime of remote computer using WMI (See related posts)

// description of your code here
Get uptime of remote computer using WMI, I'm not sure the convert time function is correct, it gives different results than psinfo.exe

Private Function GetUptime() As String


        Dim WMIScope As Management.ManagementScope
        Dim WMIConnectionOptions As New Management.ConnectionOptions
        Dim strRootDomain As String
        Dim objRootLDAP As DirectoryEntry
        Dim query As ManagementObjectSearcher
        Dim queryCollection As ManagementObjectCollection
        Dim oq As New System.Management.ObjectQuery
        Dim mo As New ManagementObject
        Dim perftimestamp As String = ""
        Dim perttimefreq As String = ""
        Dim counter As String = ""
        Dim iUptimeInSec As Long = 0

        With WMIConnectionOptions
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            .Authentication = System.Management.AuthenticationLevel.Packet

        End With

        WMIScope = New Management.ManagementScope("\\" & _
        Strcomputer & "\root\cimv2", WMIConnectionOptions)

        objRootLDAP = New DirectoryEntry("LDAP://RootDSE")
        strRootDomain = objRootLDAP.Properties.Item("rootDomainNamingContext").Value.ToString

        oq = New System.Management.ObjectQuery("select * from Win32_PerfRawData_PerfOS_System")
        query = New ManagementObjectSearcher(WMIScope, oq)
        queryCollection = query.Get()

        For Each oReturn As ManagementObject In queryCollection
            perftimestamp = oReturn("Timestamp_Object").ToString()
            perttimefreq = oReturn("Frequency_Object").ToString()
            counter = oReturn("SystemUpTime").ToString()
        Next

        ' Calculation in seconds:
        iUptimeInSec = Convert.ToInt64((Double.Parse(perftimestamp) - Double.Parse(counter)) / Double.Parse(perttimefreq))

        ' convert the seconds
        Return ConvertTime(iUptimeInSec)

    End Function

    Private Function ConvertTime(ByVal seconds As Long) As String

        Dim ConvSec As Long
        Dim ConvMin As Long
        Dim ConvHour As Long
        Dim ConvDays As Long

        Math.DivRem(seconds, 60, ConvSec)

        Math.DivRem(seconds, 3600, ConvMin)
        ConvMin = ConvMin / 60

        Math.DivRem(seconds, (3600 * 24), ConvHour)
        ConvHour = ConvHour / 3600

        ConvDays = (seconds / (3600 * 24))

        Return (ConvDays.ToString() + " days " + ConvHour.ToString() + " hours " + ConvMin.ToString() + " minutes " + ConvSec.ToString() + " seconds ")

    End Function

'or you could try this method which still doesn't match psinfo or systeminfo but it is closer

Private Function GetUptime2() As String

        Dim WMIScope As Management.ManagementScope
        Dim WMIConnectionOptions As New Management.ConnectionOptions
        Dim strRootDomain As String
        Dim objRootLDAP As DirectoryEntry
        Dim query As ManagementObjectSearcher
        Dim queryCollection As ManagementObjectCollection
        Dim oq As New System.Management.ObjectQuery
        Dim mo As New ManagementObject
        Dim perftimestamp As String = ""
        Dim perttimefreq As String = ""
        Dim counter As String = ""
        Dim iUptimeInSec As Long = 0
        Dim LastBootUpTime As DateTime
        Dim dateNow As DateTime = Date.Now

        With WMIConnectionOptions
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            .Authentication = System.Management.AuthenticationLevel.Packet

        End With

        WMIScope = New Management.ManagementScope("\\" & _
        Strcomputer & "\root\cimv2", WMIConnectionOptions)

        objRootLDAP = New DirectoryEntry("LDAP://RootDSE")
        strRootDomain = objRootLDAP.Properties.Item("rootDomainNamingContext").Value.ToString

        oq = New System.Management.ObjectQuery("Select * from Win32_OperatingSystem")

        query = New ManagementObjectSearcher(WMIScope, oq)
        queryCollection = query.Get()

        For Each oReturn As ManagementObject In queryCollection

            LastBootUpTime = System.Management.ManagementDateTimeConverter.ToDateTime(oReturn("LastBootUpTime").ToString)
            
        Next

        Dim ts As TimeSpan = dateNow.Subtract(LastBootUpTime)
        Console.WriteLine(ts.Days.ToString + " days " + ts.Hours.ToString + " hours " + ts.Minutes.ToString + " minutes " + ts.Seconds.ToString + " seconds ")

    End Function


You need to create an account or log in to post comments to this site.


Click here to browse all 4856 code snippets

Related Posts