Solved

Any way to tell is somone is remoted into a computer using XP with .net 2.0?

Posted on 2009-07-07
7
455 Views
Last Modified: 2013-11-07
I'm working on a script that users would log into a terminal server.  From there they will see test workstations that are available for them to remote desktop to.  I need to know how to tell if there is a remote desktop connection available or if someone else is using it for these test workstations.  Any ideas how I can check for an active remote desktop session running on XP with .net 2.0?  Thanks, Chad
0
Comment
Question by:chadmanvb
  • 5
  • 2
7 Comments
 
LVL 4

Expert Comment

by:mysteriousguy
Comment Utility
didn't try, but maybe WMI helps you (add a refernce to System.Management)
System.Management.ManagementScope ms = new System.Management.ManagementScope();

System.Management.ObjectQuery oq = new System.Management.ObjectQuery("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10");

System.Management.ManagementObjectSearcher oSearcher = new System.Management.ManagementObjectSearcher(ms, oq);

System.Management.ManagementObjectCollection oReturnCollection = oSearcher.Get();
 

foreach (System.Management.ManagementObject oReturn in oReturnCollection)

{

    Debug.WriteLine("Name : " + (oReturn["Name"] ?? "").ToString());

    Debug.WriteLine("StartTime: " + (oReturn["StartTime"] ?? "").ToString());

    Debug.WriteLine("Caption: " + (oReturn["Caption"] ?? "").ToString());

    Debug.WriteLine("LogonId: " + (oReturn["LogonId"] ?? "").ToString());

    Debug.WriteLine("LogonType: " + (oReturn["LogonType"] ?? "").ToString());

}

Open in new window

0
 
LVL 4

Accepted Solution

by:
mysteriousguy earned 500 total points
Comment Utility
I just tried my solution. The complete code is attached
//create a local management scope

ManagementScope managementScope = new ManagementScope();

//create a query for all available sessions (LogonType 2 = Interactive; LogonType 10 = RemoteInteractive)

ObjectQuery sessionQuery = new ObjectQuery("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10");

// execute the query

ManagementObjectSearcher sessionObjectSearcher = new ManagementObjectSearcher(managementScope, sessionQuery);

ManagementObjectCollection sessionObjectCollection = sessionObjectSearcher.Get();
 

//iterate through all sessions

foreach (ManagementObject sessionObjectItem in sessionObjectCollection)

{

    //determine properties

    uint logonType = (uint)sessionObjectItem["LogonType"];

    string logonId = sessionObjectItem["LogonId"] as string ;
 

    if (logonType == 2)

    {

        Debug.WriteLine("Logon Type: Console");

    }

    else

    {

        Debug.WriteLine("Logon Type: RDP/Terminal Server");

    }
 

    //create a new query to get the user information for the selected session

    ObjectQuery associatorQuery = new ObjectQuery(string.Concat("Associators of {Win32_LogonSession.LogonId=", logonId, "} Where AssocClass=Win32_LoggedOnUser Role=Dependent"));

    // execute the query

    ManagementObjectSearcher associatorObjectSearcher = new ManagementObjectSearcher(managementScope, associatorQuery);

    ManagementObjectCollection associatorObjectCollection = associatorObjectSearcher.Get();
 

    //iterate through all users (should only be one result per session)

    foreach (ManagementObject associatorObjectItem in associatorObjectCollection)

    {

        //determine properties and output the result

        string user = associatorObjectItem["Name"] as string;

        string name = associatorObjectItem["FullName"] as string;

        string domain = associatorObjectItem["Domain"] as string;

        Debug.WriteLine(string.Concat("User: ", user));

        Debug.WriteLine(string.Concat("FullName: " , name));

        Debug.WriteLine(string.Concat("Domain: ", domain));

    }
 

    Debug.WriteLine(string.Concat("Session start time: ", sessionObjectItem["StartTime"]));

}

Open in new window

0
 

Author Comment

by:chadmanvb
Comment Utility
Great!  I will give it a shot tomorrow at work.  Would you happen to know how to change this from c# to vb easily?  Thanks again, Chad
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:chadmanvb
Comment Utility
I just tried it, but I guess I am not sure how to get this working in VB.  I imported System.Management.  Any help would be great.  Chad
0
 

Author Comment

by:chadmanvb
Comment Utility
Was anyone able to change this from C# to vb?  Thanks, Chad
0
 

Author Comment

by:chadmanvb
Comment Utility
Ok, I have half of it working, but not sure why the other part is not working.  Below is what I have and the first part works great.

  Dim managementScope As New ManagementScope("\\" & TextBox1.Text & "\root\cimv2")
        Dim ojectQuery As New ObjectQuery("Select * from Win32_LogonSession Where LogonType = 2 OR LogonType = 10")
        Dim sessionObjectSearcher As New ManagementObjectSearcher(managementScope, ojectQuery)
        Dim sessionObjectCollection As ManagementObjectCollection = Nothing
        sessionObjectSearcher.Options.Timeout = New System.TimeSpan(0, 0, 5)
        sessionObjectSearcher.Options.ReturnImmediately = True

        sessionObjectCollection = sessionObjectSearcher.Get()


        Dim ManagementObject As ManagementObject
        Dim loginType As String = ""
        Dim strDomain As String = ""

        For Each ManagementObject In sessionObjectCollection

            loginType = ManagementObject("LogonType")
            If loginType = "2" Then
                loginType = "Console"
            Else
                loginType = "Remote Desktop"
            End If

            Exit For
        Next

        MessageBox.Show(loginType)

'*****************everything above here works!!!**********************************


        'create a new query to get the user information for the selected session
        'ObjectQuery associatorQuery = new ObjectQuery(string.Concat("Associators of {Win32_LogonSession.LogonId=", logonId, "} Where AssocClass=Win32_LoggedOnUser Role=Dependent"));
        Dim associatorQuery As New ObjectQuery(String.Concat("Associators of {Win32_LogonSession.LogonId=", logonId, "} Where AssocClass=Win32_LoggedOnUser Role=Dependent"))
'???????????????????????????????????????????????????
        'ObjectQuery(associatorQuery = New ObjectQuery(String.Concat("Associators of {Win32_LogonSession.LogonId=", logonId, "} Where AssocClass=Win32_LoggedOnUser Role=Dependent")))
        'execute the query
        Dim associatorObjectSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(managementScope, associatorQuery)
        Dim associatorObjectCollection As ManagementObjectCollection = Nothing
        associatorObjectCollection = associatorObjectSearcher.Get()

        Dim user As String = ""
        Dim name As String = ""
        Dim domain As String = ""


        'iterate through all users (should only be one result per session)
        For Each ManagementObject In associatorObjectCollection

            'determine properties and output the result
            user = associatorObjectItem("Name")
            name = associatorObjectItem("FullName")
            domain = associatorObjectItem("Domain")
'?????????????????still have errors for these 3 items.

            Exit For

        Next
0
 

Author Closing Comment

by:chadmanvb
Comment Utility
Works great, I will just try to do this in C#.  Been looking for an excuse to learn it.  Chad
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Default parameter problem in C# 3 25
Code enhancement 5 12
Clone Visual studio 2013 solution 5 15
Form design in vb.net 7 17
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now