?
Solved

C# Get Remote Computer Current User

Posted on 2012-03-20
8
Medium Priority
?
4,583 Views
Last Modified: 2013-12-14
I am trying to run a C# code against a list of live computers and then export the current logged in username in a text file next to the computer name. Can anyone hemo me to figure this out. Please....

Paul
0
Comment
Question by:pauledwardian
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
8 Comments
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 1500 total points
ID: 37746159
0
 

Author Comment

by:pauledwardian
ID: 37747512
That code gave me a lot of unknown variable. Please look at the screenshot in the attachment :

ManagementScope msc = new ManagementScope("\\\\" + IP of the OTHER>computer + "\\root\\cimv2");
 string queryString = "select LogonId from win32_logonsession wherelogontype = 2";
 query = new ManagementObjectSearcher(msc, new SelectQuery(queryString));
 foreach( ManagementObject mo in query.Get())
 {
  UserLogonId = mo["LogonId"].ToString(); // WORKS !!!!!!
 RelatedObjectQuery relatedQuery = new RelatedObjectQuery("associators of {Win32_LogonSession.LogonId='" + mo["LogonId"]+ "'}WHERE AssocClass =  Win32_LoggedOnUser");
 ManagementObjectSearcher searcher = new ManagementObjectSearcher(msc,
relatedQuery);
 foreach (ManagementObject mob in searcher.Get())
  {
  Username = mob["Caption"] + " " + mob["Domain"] + " " +
mob["Name"]; // DOESN'T WORK !!!!!!! they are always empty strings
 }
}
0
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37747544
Did you try my solutions???
0
 

Accepted Solution

by:
pauledwardian earned 0 total points
ID: 37747625
Also, this code gives me the username on the current omputer regardless of what I put in the machine name:

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using SetEnv;
 

namespace GetUserInfo
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            string CurrLocUser = string.Empty;
            SetLocEnvironmentVariable UserInfo = new SetLocEnvironmentVariable();
           
            // Create a sample environment variable and set its value (for the current process).
            //SampleSetEnvironmentVariable.SetEnvironmentVariableEx("TESTENV", "TestValue");
 
            CurrLocUser = UserInfo.GetCurrUserInfo("XYZ123"); // System name
 
            // Verify that environment variable is set correctly.
            Console.WriteLine("Current local user: " + CurrLocUser);
 
            return;
 
        }
    }
}
 




GET/SET ENVIRONMENT CODE FILE
-------------------------------
 

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
 

namespace SetEnv
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 public class SetLocEnvironmentVariable
 {
        // Import the kernel32 dll.
        [DllImport("kernel32.dll",CharSet=CharSet.Auto, SetLastError=true)]
 
        [return:MarshalAs(UnmanagedType.Bool)]
 
        // The declaration is similar to the SDK function
        public static extern bool SetEnvironmentVariable(string lpName, string lpValue);
        public string GetCurrUserInfo(string SystemName)
        {
            string CurrentUser = string.Empty;
 

            CurrentUser = Environment.UserName;
            return CurrentUser;
 
        }
 }
}
0
 

Author Closing Comment

by:pauledwardian
ID: 37923303
Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
Suggested Courses

752 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