tags:

views:

314

answers:

1

Hi, I would like to know if their is a method which I can call which can tell me if a PowerShell snapin is installed.

I know I could call probably do it, say via WMI or write a PowerShell script and return a list to C#, but is their not a method do it somewhere.

Thanks.

+1  A: 

I'm not sure if this is the optimal way to do it but take a look at the default runspace configuration e.g.:

using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;

var cmdlets = Runspace.DefaultRunspace.RunspaceConfiguration.Cmdlets;
var snapins = (from cmdlet in cmdlets
              select new { cmdlet.PSSnapin.Name }).Distinct();

Compiled by hand so YMMV.

To see which snapins are installed, instead of loaded, enumerate the contents of this registry key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns

Or you could invoke Get-PSSnapin -Registered from the C# code and process the PSSnapInInfo objects that are returned.

Keith Hill
Hi Keith,I'll test it.Thanks.Bob.
scope_creep
Kieth, Unfortunately that doesn't work. That script, when fixed, only displays the current spanins that are loaded into the runspace when the runspace is opened. Effectively the ones which are part of the powershell install. Thanks for the effort.Bob.
scope_creep
Ah, in that case, just enumerate the contents of the regkey HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\PowerShellSnapIns
Keith Hill
Hi Kieth,That doesn't work either, as that registry key, is only populated when you have added a windows installer installed snapin into a runspace.I know their is a WMI call which evaluates all windows installer installed products, but if you install a snapin using a 3rd party installer, then WMI will not pick it up. Which leaves a whole. Any other ideas? I think if their is nothing else, there is no simple answer.Bob
scope_creep
Hmm both Get-PSSnapin and this registry key will show you all *installed* snapins on that machine (hence the location - HKEY_LOCAL_MACHINE). This is irrespective of runspace. BTW when I say *installed* I don't mean loaded via Add-PSSnapin. I mean that you either ran a setup.exe (or .msi) file to install the snapin or you manually ran installutil.exe on the snapin's DLL. IOW, before you can load a snapin via Add-PSSnapin, the snapin has to be registered with PowerShell (ie has an entry under the registry key shown above).
Keith Hill
Hi Keith, Yea I know what you mean. Well I downloaded the IIS 7 WebAdministration and installed it, but when I check that registry key, their is no sign of it. I'n using V2 CTP 3. That wouldn't make any difference, would it.
scope_creep
Apologies. I must be sleeping. Found it. Thanks.
scope_creep
`Runspace.DefaultRunspace.RunspaceConfiguration.Cmdlets.Selected(c => c.PSSnapin.Name).Distinct()`
abatishchev