Link to home
Start Free TrialLog in
Avatar of zoscoit
zoscoitFlag for Saudi Arabia

asked on

getservices() to a remote server C#

See attached image for general idea about the structure...

I have a c# website built on IIS on SERVER01, and the website webform has the function as attached in image 1

public static string[,] ServiceInstalledList(string machineName)
        { 
            // get list of Windows services
            ServiceController[] services = ServiceController.GetServices(machineName);
            string[,] serviceList = new string[services.Length, 5]; 
            int i=0;
            // try to find service name
            foreach (ServiceController service in services)
            {
                serviceList[i, 0] = service.ServiceName;
                serviceList[i, 1] = service.Status.ToString();
                serviceList[i, 2] = service.DisplayName.ToString();
                serviceList[i, 3] = service.MachineName.ToString();
                serviceList[i, 4] = service.StartType.ToString();
                i = i + 1;
                // if (service.ServiceName == serviceName)
                //    return true; 
            } 
            return serviceList;
        }

Open in new window



It is working as expected when the "machineName" is local "SERVER01", it shows the list services on the server.

But when I change the machineName to a remote server (server host name or server IP address) in the same network (10.0.0.0/24),  I got the error attached in the send image.

The error shows the website can reach to the server and can see it, but it does not have the access only. It is something related to security, passwords ...etc.



[Win32Exception (0x80004005): Access is denied]

[InvalidOperationException: Cannot open Service Control Manager on computer 'SERVER02'. This operation might require other privileges.]
   System.ServiceProcess.ServiceController.GetDataBaseHandleWithAccess(String machineName, Int32 serviceControlManaqerAccess) +51781
   System.ServiceProcess.ServiceController.GetServicesOfType(String machineName, Int32 serviceType) +183

Open in new window

123.PNG
122.PNG
124.PNG
Avatar of ste5an
ste5an
Flag of Germany image

As the error message tells you, the account your code is run under requires the necessary Windows privileges to connect to another machines service manager.

From the security perspective, this means that you either run this part in a different app pool under an account with the necessary privileges or you need to impersonate that code part.

Both require that you have such an account with that privileges.
Avatar of zoscoit

ASKER

What do you suggest to do ?
I want to make a web page that shows the services on the servers on the "cloud network". And this is the way I designed it.

Do you suggest another easier way ?
122.PNG
Sounds like normal monitoring.

Normally an agent (Windows service or a script as scheduled task) would gather the necessary information and store it in a central database, where it is read by your application.

This would avoid the possible securitiy issues by having more privileges than necessary on your IIS server.
Avatar of zoscoit

ASKER

That was my first method, but I was stuck at the point of scheduling the task, it is something related to security I guess as well.

 
Get-Service | Out-File C:\Users\xxxxx\Documents\ServerStatus\SERVERNAME _Services.txt

Open in new window



When I run the windows scheduled task manually, I can get the services list exported as txt as expected.

But when I schedule the PowerShell file, the exported text file is empty.

Attached images.
121.png
ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial