Jesse
Jesse

Reputation: 1940

Windows service records wrong username

I've installed a Windows Service on several computers. I had to logon on as a local admin because the user didnt have local admin rights. Now the problem is say on my computer with local admin rights it logs my Username just fine, but on the computers where I had to install from Admin it Logs Administrator. I'm Using WMI to get this information.

 ConnectionOptions oConn = new ConnectionOptions();
        System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\\\localhost", oConn);


        System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select * from Win32_ComputerSystem");
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
        ManagementObjectCollection oReturnCollection = oSearcher.Get();

        foreach (ManagementObject oReturn in oReturnCollection)
        {
            userName = oReturn["UserName"].ToString().ToLower().Split('\\')[1];
        }

This is really got me confused. They are logged on, but yet it show Administrator? Anyone able to clarify why this is happening.

Upvotes: 0

Views: 229

Answers (1)

Oded
Oded

Reputation: 498934

If you want the service to log your name you need to install it with your credentials.

Services are running as the user that they have been installed with - they have no idea about logged in users (what if there are several users logged in at the same time - which one should they be recording?).

The whole point of a windows service is that it runs without the need for anyone to login.

Upvotes: 3

Related Questions