MSDN Magazine - December 2007 - (Page 76) Figure 7 Snap-In for My Custom Cmdlets // This class defines the properties of a snapin [RunInstaller(true)] public class IsolatedStorageCmdlets : PSSnapIn { /// Creates an instance of DemoSnapin class. public IsolatedStorageCmdlets() : base() { } /// The snap-in name that is used for registration public override string Name { get { return “IsolatedStorageCmdlets”; } } /// Gets vendor of the snap-in. public override string Vendor { get { return “James W. Truher”; } } /// Gets description of the snap-in. public override string Description { get { return “Isolated Storage Cmdlets”; } } /// The format file for the snap-in. private string[] _formats = { “IsolatedStorage.Format.ps1xml” }; public override string[] Formats { get { return _formats ; } } } Figure 8 Sample Format File IsolatedStorage IsolatedStorageSnapin.IsolatedStorageData Key 12 Key Value a subset of the cmdlets and providers in one assembly or when you want to register cmdlets and providers that are in different assemblies. The code for my snap-in is shown in Figure 7. It’s quite straightforward. I simply override the appropriate members and then I’m pretty much finished. Formatting Notice in Figure 7 that there is a value for the Formats member of this snap-in. This allows you to create formatting directives for the objects that the cmdlets emit. In this case, I have a simple object, but I don’t want to present all the members by default—I want to be sure that I print only the information that is useful, which in this case is the key and the value. The creation of these format files could fill an article by itself, and the topic is beyond the scope of this article. Figure 8 provides sample code that creates a table format with the two columns I’m interested in: the key and value of the IsolatedStorageData object. For the purposes of this example, you can just create a file named IsolatedStorage.Format.ps1xml in the same directory as the assembly and when the snap-in is loaded, you shouldn’t see any errors. Installing and Loading a PSSnapIn Installing a snap-in is extremely easy. Just run Installutil.exe with the path to your assembly. When this utility runs, it creates some registry entries under HKLM\SOFTWARE\Microsoft\ PowerShell\1\PowerShellSnapins\ . When Windows PowerShell loads a snap-in, these entries are used to load the assembly and find the various configuration files. It is also worth mentioning that Installutil.exe is the recommended installation method during development only, as the utility’s actions don’t properly register their uninstallation dependencies. For installation in production environments, you Figure 9 Registering the Snap-In 76 msdnmagazine Custom Cmdlets
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.