Module Manifests in CTP3

Module Manifests are wonderful things. Basically, you can set up all kinds of dependencies and create versioning with them, among many other great features.

I just wanted to get a quick example out to show how you can create them. First, you need a module. To create a module, just rename a script from .PS1 to .PSM1.

Here's a very simple PSM1 file

38 >  cat foobar.psm1
function foo {"foo"}
function bar {"in bar and calling foo";foo}
export-modulemember bar

Note the "export-modulemembr cmdlet. This cmdlet says which functions are public and available to the user that imports this module.

Now we can create a new module manifest using the New-ModuleManifest Cmdlet.

39 >  new-modulemanifest
cmdlet New-ModuleManifest at command pipeline position 1
Supply values for the following parameters:
Path: foobar.psd1
NestedModules[0]: foobar.psm1
Author: Andy Schneider
CompanyName: Get-PowerShell
Copyright: All Rights reserved
Description: PowerShell Modules Rock

One trick to note here, even though you are prompted for a bunch of information, not all of these parameters are required. If you don't want to enter a NestedModule, you can just hit enter and it will prompt you for the next parameter.

This generates a PSD1 file which is just a hash table. Here's what this auto-generated one looks like

44 >  cat .\foobar.psd1
# Module manifest for module 'foobar'
# Generated by: Andy Schneider
# Generated on: 12/22/2008
# These modules will be processed when the module manifest is loaded.
NestedModules = 'foobar.psm1'
# This GUID is used to uniquely identify this module.
GUID = '27c7c84b-dbce-4919-8494-aa4eb4646915'
# The author of this module.
Author = 'Andy Schneider'
# The company or vendor for this module.
CompanyName = 'Get-PowerShell'
# The copyright statement for this module.
Copyright = 'All Rights reserved'
# The version of this module.
ModuleVersion = '1.0'
# A description of this module.
Description = 'PowerShell Modules Rock'
# The minimum version of PowerShell needed to use this module.
PowerShellVersion = '2.0'
# The CLR version required to use this module.
CLRVersion = '2.0'
# Functions to export from this manifest.
ExportedFunctions = '*'
# Aliases to export from this manifest.
ExportedAliases = '*'
# Variables to export from this manifest.
ExportedVariables = '*'
# Cmdlets to export from this manifest.
ExportedCmdlets = '*'
# This is a list of other modules that must be loaded before this module.
RequiredModules = @()
# The script files (.ps1) that are loaded before this module.
ScriptsToProcess = @()
# The type files (.ps1xml) loaded by this module.
TypesToProcess = @()
# The format files (.ps1xml) loaded by this module.
FormatsToProcess = @()
# A list of assemblies that must be loaded before this module can work.
RequiredAssemblies = @()
# Lists additional items like icons, etc. that the module will use.
OtherItems = @()
# Module specific private data can be passed via this member.
PrivateData = ''
45 >

Now finally we can import the module and run get-module to see what we have


   1: 45 >  import-module .\foobar.psd1
   2: 46 >  get-module *foo*
   5: Name              : C:\Users\andys\Documents\Scripts\foobar.psd1
   6: Path              : C:\Users\andys\Documents\Scripts\foobar.psd1
   7: Description       : PowerShell Modules Rock
   8: Guid              : 27c7c84b-dbce-4919-8494-aa4eb4646915
   9: Version           : 1.0
  10: ModuleBase        : C:\Users\andys\Documents\Scripts
  11: ModuleType        : Manifest
  12: PrivateData       :
  13: AccessMode        : ReadWrite
  14: ExportedAliases   : {}
  15: ExportedCmdlets   : {}
  16: ExportedFunctions : {[bar, bar]}
  17: ExportedVariables : {}
  18: NestedModules     : {foobar.psm1}
  22: 47 >  foo
  23: The term 'foo' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again
  24: .
  25: At line:1 char:4
  26: + foo <<<<
  27:     + CategoryInfo          : ObjectNotFound: (foo:String) [], CommandNotFoundException
  28:     + FullyQualifiedErrorId : CommandNotFoundException
  30: 48 >  bar
  31: in bar and calling foo
  32: foo
  33: 49 >

Note the Exported Functions property. When I created this Module Manifest, I said that it required foobar.psm1 by specifying a Nested Module. Also remember that I only exported the function bar, not foo, in foobar.psm1. You can see that foo does not work but bar runs swimmingly, and can even call into foo, even though the user doesn't have access to that function.

This is really only the beginning when it comes to Modules.

Comments (2) -

David Noble 3/14/2009 8:03:38 PM

Fricking fantastic! CTP3 is a joy. Keep up the good work.

CTP3 is gooooodd...

Comments are closed