/// <summary>
        /// Installs a service on any machine
        /// </summary>
        /// <param name="machineName">Name of the computer to perform the operation on</param>
        /// <param name="name">The name of the service in the registry</param>
        /// <param name="displayName">The display name of the service in the service manager</param>
        /// <param name="physicalLocation">The physical disk location of the executable</param>
        /// <param name="startMode">How the service starts - usually Automatic</param>
        /// <param name="userName">The user for the service to run under</param>
        /// <param name="password">The password fo the user</param>
        /// <param name="dependencies">Other dependencies the service may have based on the name of the service in the registry</param>
        /// <param name="interactWithDesktop">Should the service interact with the desktop?</param>
        /// <returns>A service return code that defines whether it was successful or not</returns>
        public ServiceReturnCode Install(string machineName, string name, string displayName, string physicalLocation, ServiceStartMode startMode, string userName, string password, string[] dependencies, bool interactWithDesktop)
        {
            const string methodName = "Create";

            //string[] serviceDependencies = dependencies != null ? dependencies.Split(',') : null;
            if (userName.IndexOf('\\') < 0)
            {
                //userName = "******" + userName;
                //UNCOMMENT the line above - it caused issues with color coding in THIS ARTICLE
            }

            try
            {
                object[] parameters = new object[]
                {
                    name,                                              // Name
                    displayName,                                       // Display Name
                    physicalLocation,                                  // Path Name | The Location "E:\somewhere\something"
                    Convert.ToInt32(ServiceType.OwnProcess),           // ServiceType
                    Convert.ToInt32(ServiceErrorControl.UserNotified), // Error Control
                    startMode.ToString(),                              // Start Mode
                    interactWithDesktop,                               // Desktop Interaction
                    userName,                                          // StartName | Username
                    password,                                          // StartPassword |Password
                    null,                                              // LoadOrderGroup | Service Order Group
                    null,                                              // LoadOrderGroupDependencies | Load Order Dependencies
                    dependencies                                       // ServiceDependencies
                };
                return((ServiceReturnCode)_wmi.InvokeStaticMethod(machineName, CLASS_NAME, methodName, parameters));
            }
            catch
            {
                return(ServiceReturnCode.UnknownFailure);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Instala un servicio en una maquina
        /// </summary>
        /// <param name="machineName">nombre de la maquina</param>
        /// <param name="name">nombre del servicio</param>
        /// <param name="displayName">El nombre que se mostrara en la consola de administracion</param>
        /// <param name="physicalLocation">el path de instalacion</param>
        /// <param name="startMode">Como arrancara el servicio - normalmente: Automatic</param>
        /// <param name="userName">el usuario que ejecuta el servicio</param>
        /// <param name="password">el password del usuario</param>
        /// <param name="dependencies">Otras dependencias del servicio</param>
        /// <param name="interactWithDesktop">Interactua con el escritorio?</param>
        /// <returns>Codigo devuelto indicando el exito de la operacion</returns>
        public Win32API.ServiceReturnCode Install(string machineName, string name,
                                                  string displayName, string physicalLocation, Win32API.ServiceStartMode startMode,
                                                  string userName, string password, string[] dependencies, bool interactWithDesktop)
        {
            const string methodName = "Create";

            //string[] serviceDependencies = dependencies != null ? dependencies.Split(',') : null;
            //if (userName.IndexOf('\\') < 0)
            //{
            //    //userName = "******" + userName;
            //    //
            //}

            try
            {
                object[] parameters = new object[]
                {
                    name,                                                       // Nombre
                    displayName,                                                // nombre mostrado
                    physicalLocation,                                           // Path
                    Convert.ToInt32(Win32API.ServiceType.OwnProcess),           // Tipo de servicio
                    Convert.ToInt32(Win32API.ServiceErrorControl.UserNotified), // Error Control
                    startMode.ToString(),                                       // Modo arranque
                    interactWithDesktop,                                        // interaccion con el escritorio
                    userName,                                                   // usuario
                    password,                                                   // Password
                    null,                                                       //
                    null,                                                       // Dependencias
                    dependencies                                                // ServiceDependencies
                };
                return((Win32API.ServiceReturnCode)_wmi.InvokeStaticMethod(machineName, CLASS_NAME, methodName, parameters));
            }
            catch
            {
                return(Win32API.ServiceReturnCode.UnknownFailure);
            }
        }