使用此 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()
}
}
}
这是我的 installshield 脚本,它创建了我的 Windows 服务安装。IE。如果系统已经安装了我的 windows 服务,当他们再次重新安装时,我需要确保我需要指向运行 windows 服务的任何用户。(如果它的本地系统应该指向本地系统,如果它是一个特定的用户,比如有密码的shareye500,那么它应该指向shareye500(用户)并弹出(如设置服务登录)密码应该显示)。
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。