简体   繁体   中英

Installshield winforms service creation with specific logon user account

With this c# code i can get the user of my windows service

        System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select Name,startname from Win32_Service where name ='MyWindowsServices' ")); 

            using (System.Management.ManagementObjectSearcher mgmtSearcher = new System.Management.ManagementObjectSearcher(sQuery))
            {
                foreach (System.Management.ManagementObject service1 in mgmtSearcher.Get())
                {

            //If not local system , then associated with different user                                  
             if(service1["startname"].ToString().ToLower()!="localsystem")
                               {
                             service1["startname"].ToString()).ToString()
                               }
                   }
               }

This is my installshield script that creates my windows service installation. ie. If the system has already my windows service installed, when they re-install again I need to make sure i need to point back whichever the user that the windows service was running under. (if its localsystem it should point localsystem, if its a specific user like sharpeye500 with some password, then it should point back to sharpeye500 (user) & pop up (like set service login) for password should be displayed).

Set objCreate = objWMIService.Get("Win32_BaseService")


Get Window Service 
Set colListOfServices = objWMIService.ExecQuery _
 ("Select * from Win32_Service Where Name = 'MyWindowsServices'")
'First Stop and Delete service and then install
For Each objService in colListOfServices  

//I get the user name associated with the service

 startName=objService.StartName   
    objService.StopService()   
    Next   

'I have delete service logic & then i install Window Service again

objCreate.Create "MyWindowsServices" ,"My Windows Service Description" ,
"c:\test\" & "MyWindowsServices.exe", OWN_PROCESS, NORMAL_ERROR_CONTROL,
 "Automatic", NOT_INTERACTIVE, startName, ""  

However when i pass startName in the above syntax, my installation fails, but if i pass Null instead, it creates a windows service installation with localsystem user account.

How do i get the user account associated with the windows service & assign it back on the installation via installshield script?


  System.Management.SelectQuery sQuery = new System.Management.SelectQuery(string.Format("select startname from Win32_Service where name ='MyWindowsService' ")); 
            using (System.Management.ManagementObjectSearcher mgmtSearcher = new System.Management.ManagementObjectSearcher(sQuery))
            {
                foreach (System.Management.ManagementObject service in mgmtSearcher.Get())
                {
                    if (service["startname"].ToString().ToLower() != "localsystem" || service["startname"].ToString().ToLower() != "localservice")
                    {
                        this.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.User;
                        this.ServiceProcessInstaller1.Username = service["startname"].ToString();
                    }
                    else
                    {

                        this.ServiceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalSystem;
                        this.ServiceProcessInstaller1.Password = null;
                        this.ServiceProcessInstaller1.Username = null;
                    }

                }
            }

I have this logic, all i need to do is put this in installscript (in vb), however i am struggling how to use ManagementObject in installscript.

That approach won't work. First, while you can readily get the username for a service's account, you can't call a method to get the password of the account (an obvious security restriction).

Deleting the service will go smoothly but the Create method requires both the username & password for any account other than LocalSystem (again, for security) which you won't fully have. This will be a problem for any account other than LocalSystem.

Why do you have to delete & re-install your service? Can you simply stop the service and update the underlying executables/dlls that the service invokes? You can even change the service's command line with API functions...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM