April 25th, 2006

The worlds easiest way to create/install MshSnapins

PowerShell Team
PowerShell Team

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

[Edit: Monad has now been renamed to Windows PowerShell. This script or discussion may require slight adjustments before it applies directly to newer builds.]

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.