簡體   English   中英

使用特定的登錄用戶帳戶創建Installshield winforms服務

[英]Installshield winforms service creation with specific logon user account

使用此C#代碼,我可以讓我的Windows服務用戶

        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()
                               }
                   }
               }

這是創建Windows服務安裝的installshield腳本。 即。 如果系統已經安裝了我的Windows服務,那么當他們再次重新安裝時,我需要確保我需要指出運行Windows服務的用戶。 (如果它的本地系統應該指向本地系統,如果它的特定用戶(例如sharpeye500)具有一些密碼,那么它應該指向sharpeye500(用戶)並彈出(例如設置服務登錄名)以顯示密碼)。

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  

//我得到與服務關聯的用戶名

 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, ""  

但是,當我以上述語法傳遞startName時,安裝失敗,但如果傳遞Null,它將使用localsystem用戶帳戶創建Windows服務安裝。

如何獲取與Windows服務關聯的用戶帳戶,並通過installshield腳本將其分配回安裝程序?


  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;
                    }

                }
            }

我有這個邏輯,我需要做的就是將其放在installscript(在vb中)中,但是我正在努力如何在installscript中使用ManagementObject。

這種方法行不通。 首先,雖然您可以輕松獲得服務帳戶的用戶名,但無法調用獲取帳戶密碼的方法(明顯的安全限制)。

刪除該服務將很順利,但是Create方法需要除LocalSystem以外的其他任何帳戶的用戶名和密碼(再次出於安全性考慮),您將無法完全擁有該帳戶。 對於除LocalSystem以外的任何帳戶,這都是一個問題。

為什么必須刪除並重新安裝服務? 您能否簡單地停止該服務並更新該服務調用的基礎可執行文件/ dll? 您甚至可以使用API​​函數更改服務的命令行...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM