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

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

Get username of logged in remote user, vb.net Win32_ComputerSystem Version

// description of your code here
This is an even shorter method of getting the remote user through WMI
Imports System.Management
Imports System.DirectoryServices
Imports Microsoft.Win32
Imports System.Runtime.InteropServices
Module Module1
    Dim objManagementClass As ManagementClass
    Dim objManagementScope As ManagementScope
    Dim objManagementBaseObject As ManagementBaseObject
    Public Strcomputer As String = ""
    Sub Main()

        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 UserDomain As String
        Dim UserID As String
        Dim StrLoggedUserSID() As Byte

        Try

            Dim Strcomputer As String = My.Application.CommandLineArgs.Item(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 UserName from Win32_ComputerSystem")
            query = New ManagementObjectSearcher(WMIScope, oq)
            queryCollection = query.Get()

            For Each oReturn As ManagementObject In queryCollection

                If Not oReturn("UserName") Is Nothing Then
                    Console.WriteLine(oReturn("UserName").ToString)
                Else
                    Console.WriteLine("Not logged in")
                End If

            Next

        Catch ex As Exception

            Console.WriteLine(ex.ToString)

        End Try

    End Sub

Get uptime of remote computer using WMI

// 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

Using Ruby & WMI to Get Win32 Process Information

From the Ruby on Windows blog...

Use Windows Management Instrumentation (WMI) to get information about the Win32 processes being run.
require 'win32ole'

wmi = WIN32OLE.connect("winmgmts://")

processes = wmi.ExecQuery("select * from win32_process")

for process in processes do
    puts "Name: #{process.Name}"
    puts "CommandLine: #{process.CommandLine}"
    puts "CreationDate: #{process.CreationDate}"
    puts "WorkingSetSize: #{process.WorkingSetSize}"
    puts
end

Further details and discussion here.


Using Ruby & WMI to Detect a USB Drive

From the Ruby on Windows blog.

How to use Windows Management Instrumentation (WMI) to determine if a "USB Mass Storage Device" is inserted.
require 'win32ole'

wmi = WIN32OLE.connect("winmgmts://")

devices = wmi.ExecQuery("Select * From Win32_USBControllerDevice")
for device in devices do
    device_name = device.Dependent.gsub('"', '').split('=')[1]
    usb_devices = wmi.ExecQuery("Select * From Win32_PnPEntity Where DeviceID = '#{device_name}'")
    for usb_device in usb_devices do
        puts usb_device.Description
        if usb_device.Description == 'USB Mass Storage Device'
            # DO SOMETHING HERE
        end
    end
end

Further details can be found here.

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS