Solved

C# Get Remote Computer Current User

Posted on 2012-03-20
8
4,472 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 500 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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 
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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

717 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