Page view counter

Check services on remote machine

Last post 07-03-2007 4:01 AM by reshmap. 5 replies.

Sort Posts:

  • Check services on remote machine

    12-20-2005, 5:27 PM
    • Loading...
    • odin_dark
    • Joined on 08-24-2005, 7:48 PM
    • Posts 10
    • Points 50

    Hello, I'm trying to check the services status on a remote machine, the code i'm using is the next:

    string MachineName = "somename"

    ServiceController[] services = ServiceController.GetServices( MachineName );

    foreach( ServiceController service in services ) {

    Console.Write(string.Format( "{0} [ {1} ]", service.ServiceName, service.Status.ToString() ) );

    }

     

    this works fine if i execute it on a windows C# application, but it doesn't work if i try to execute it on an ASP .NET WEB Application, it sends the next error when i try to get the services:

    System.InvalidOperationException: Cannot open Service Control Manager on computer 'somename'. This operation might require other privileges.

    Someone can help???

     

     


  • Re: Check services on remote machine

    12-22-2005, 4:10 AM
    The ASPNET account is a local account and has no privilidges on the remote machine. You need to use an account with permissions on the remote machine.
  • Re: Check services on remote machine

    12-22-2005, 10:49 AM
    • Loading...
    • odin_dark
    • Joined on 08-24-2005, 7:48 PM
    • Posts 10
    • Points 50
    Ok, and how can i do that? i found some ImpersonationUtil, the code is the next, but it doesn't work

    public class ImpersonationUtil {
            public static bool Impersonate( string logon, string password, string domain ) {
                WindowsIdentity tempWindowsIdentity;
                IntPtr token = IntPtr.Zero;
                IntPtr tokenDuplicate = IntPtr.Zero;

                if( LogonUser( logon, domain, password, LOGON32_LOGON_INTERACTIVE,
                    LOGON32_PROVIDER_DEFAULT, ref token) != 0 ) {

                    if ( DuplicateToken( token, 2, ref tokenDuplicate ) != 0 ) {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        impersonationContext = tempWindowsIdentity.Impersonate();
                        if ( null != impersonationContext ) return true;               
                    }           
                }

                return false;
            }

            public static void UnImpersonate() {
                impersonationContext.Undo();
            }

            [DllImport("advapi32.dll", CharSet=CharSet.Auto)]
            public static extern int LogonUser(
                string lpszUserName,
                String lpszDomain,
                String lpszPassword,
                int dwLogonType,
                int dwLogonProvider,
                ref IntPtr phToken );

            [DllImport("advapi32.dll", CharSet=System.Runtime.InteropServices.CharSet.Auto, SetLastError=true)]
            public extern static int DuplicateToken(
                IntPtr hToken,
                int impersonationLevel, 
                ref IntPtr hNewToken );

            private const int LOGON32_LOGON_INTERACTIVE = 2;
            private const int LOGON32_LOGON_NETWORK_CLEARTEXT = 4;
            private const int LOGON32_PROVIDER_DEFAULT = 0;
            private static WindowsImpersonationContext impersonationContext;
        }

  • Re: Check services on remote machine

    12-23-2005, 9:38 AM
    • Loading...
    • odin_dark
    • Joined on 08-24-2005, 7:48 PM
    • Posts 10
    • Points 50
    ok. now i found the way to do it, we just have to put the next code in the web.config file:

    <identity impersonate="true" userName="userName" password="password" />

    obviously the user must have access to these services on the remote machine

    Thanks to all!
     

  • Re: Check services on remote machine

    02-13-2006, 4:03 PM
    • Loading...
    • jballhrb
    • Joined on 09-08-2004, 4:31 PM
    • Posts 79
    • Points 415

    How is this accomplished in a Web Application? I am trying to use the code in one, and am unsuccessful. Does it only work in a Windows Application and not a Web Application?

    Can you port the entire code for your aspx page?

  • Re: Check services on remote machine

    07-03-2007, 4:01 AM
    • Loading...
    • reshmap
    • Joined on 07-03-2007, 3:39 AM
    • Posts 1
    • Points 2

    Hello,

               I was using the same code to check for the services through windows application using c#.net and it was working fine. Recently I had changed one of my service names and since then its giving the error

    Cannot open Service Control Manager on Computer "ComputerName". This operation might required other privileges.

    And my account has administrator permissions on the remote computer.

    Any help is appreciated.

    Thanks
     

Page 1 of 1 (6 items)