joebalt
joebalt

Reputation: 989

Retrieve MSI product code for an installed Service

I've looked around a fair amount for an answer to this, hopefully my "google-fu" is not the issue...anyhow.

I want to enumerate all the services on a Windows (2003 R2, 2008, 2008 R2) machine and then determine what their product codes are to determine what MSI Package (in our maintained repository) was used to install that Windows Service).

Basically, take the list that comes from something like this PS command: {Get-Service | Format-List *}, or the C# line: System.ServiceProcess.ServiceController[] services = ServiceController.GetServices();, and then iterate over all those returned service names and determine what the MSI Product Code is for each.

I'd prefer the C# route, but will take all suggestions.

Is this possible? Or is there a better way that does not involve the ServiceController class in my example?

Thanks in advance for any advice!

Upvotes: 0

Views: 1681

Answers (2)

Christopher Painter
Christopher Painter

Reputation: 55581

WiX DTF's Microsoft.Deployment.WindowsInstaller has a class called ComponentInstallation. It has a static property called AllComponents that returns an IEnumerable of ComponentInstallation.

I suppose you could query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services to get the imagepath and try to correlate it to a Component that is managed by MSI. Once you make that connection the ComponentInstallation class has a property called ClientProducts that returns IEnumerable of ProductInstallation. Remember, a component can be shared across multiple MSI's.

Once you have the ProductInstallation reference you can get it's ProductCode property.

This would catch the services that were installed by MSI even if they were using InstallUtil because you are tracking who laid down the file.

Upvotes: 3

Phil Bolduc
Phil Bolduc

Reputation: 1656

Not all Windows Services are installed using a MSI. You can install a .NET service using installutil.exe. Windows Services are registered in HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services. As the registry does not hold MSI product numbers, I think you would need to get the ImagePath property and cross reference that items installed by the MSI package. The MSI data is stored in HKEY_CLASSES_ROOT\Installer. How to link these two up is beyond me.

Upvotes: 2

Related Questions