The worlds easiest way to create/install MshSnapins
Hopefully you've read the previous entries on how to create MshSnapins to include your own cmdlets/providers without having to create an entire separate executable (custom shell).
If you didn't or if you did and you don't feel like writing the extra MshSnapin class to be used by InstallUtil.exe, you're in luck. I wrote a little ditty(script) about taking any arbitrary assembly and setting the appropriate values in the Registry for use.And in using this little handy script, you'll be able to look at registry settings to see what needs to get added to add your mshsnapin.
This script takes 2 parameters. first parameter is the assembly containing your cmdlets and/or providers. The 2nd optional parameter is the "name" of the mshsnapin. If you don't supply a name, then the name of the mshsnapin will be the assemblyfile (1st parameter)
Lets assume that you created an assembly "myawesomecmdlets.dll" which contain the most awe-inspiring cmdlets possibly imaginable.
MSH> register-mshsnapin myawesomecmdlets.dll mysnapin
MSH> add-mshsnapin mysnapin # add your snapin to the current session
MSH> create-somethingwonderful # run one of your cmdlets :)
If you run regedit, you'll then be able to see the entries that get added for your mshsnapin "mysnapin":
HKLM\Software\Microsoft\MSH\1\MshSnapins\mysnapin has values for
ApplicationBase - directoryname where your assembly is located. This is also the dir where help files, types & format files are looked for
AssemblyName - Full strong name of assembly
Description - text describing your mshsnapin
ModuleName - full path of assembly file
MshVersion - "1.0"
Version - "1.0"
So use the script at end of this post to quickly take any assembly and configure it be added as an mshsnapin.
Hope this helps!
Scott
# Beginning of script 'register-mshsnapin'
param ([string] $assemblyfile, [string] $MshSnapinName = $assemblyFile)
# Make sure we can load assembly
################################
$assemblyInfo = [System.Reflection.Assembly]::LoadFrom($assemblyFile)
if($assemblyInfo -eq $null)
{
"unable to load assembly $assemblyFile!"
exit -1
}
# Based on MshSnapinName, lets create the Reg Key values
#################################################
$keyname = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Msh\1\MshSnapins\" + $MshSnapinName
[Microsoft.Win32.Registry]::SetValue($keyname, "ApplicationBase",
[System.IO.Path]::GetDirectoryName($assemblyInfo.Location) )
[Microsoft.Win32.Registry]::SetValue($keyname, "AssemblyName",$assemblyInfo.FullName)
[Microsoft.Win32.Registry]::SetValue($keyname,"Description","A Test MshSnapin")
[Microsoft.Win32.Registry]::SetValue($keyname,"ModuleName",$assemblyInfo.Location)
[Microsoft.Win32.Registry]::SetValue($keyname,"Version","1.0")
[Microsoft.Win32.Registry]::SetValue($keyname,"MshVersion","1.0")
# End of script
Comments
- Anonymous
February 22, 2006
After loading a snapin dll through "register-mshsnapin", there was no way for me to remove the loaded snapin from showing up "get-mshsnapin -registered" unless I go to registry and delete the key under "HKLMsoftwaremicrosoftmsh1mshsnapins" manually.
When I used installutil.exe, all I had to do to unregister the assembly was through "installutil.exe -u <assembly name>".
I think having to call "installutil.exe -u" saves me more time than going to registry and deleting the registered snapin key.
Anyways, overall, the post was useful on finding out inner workings of how Snapins are registered.
One more thing, I was surprised that,
the solution was less Monad-like than I expected in a way that, the script used "[Microsoft.Win32.Registry]::SetValue" instead of Provider-indenpendent "set-property" (Oh yeah, wouldn't it be also nice to modified the script to generate "Vendor" registry key?) - Anonymous
February 24, 2006
I agree with what you're saying. Fact is that in my dev environment I rarely remove the registered MshSnapins so hadn't thought of removal consequences. that could easily be added to script.
And like you said, it was partly an educational experience about what the settings are and why?
Problem is that you may or may not be able to run InstallUtil.exe as part of your setup(for no technical reason, possibly security concern about causing arbitrary code to be executed) so I wanted to provide an alternative.
And yes, I should've posted the Registry provider way of adding Reg key values. But I also wanted to show the power of Monad and how easy it is to call static methods.... yeah that's it :) - Anonymous
June 19, 2009
PingBack from http://mydebtconsolidator.info/story.php?id=15678