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.