Archive for the “CTP 3” Category

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
NestedModules[1]:
Author: Andy Schneider
CompanyName: Get-PowerShell
Copyright: All Rights reserved
Description: PowerShell Modules Rock
TypesToProcess[0]:
FormatsToProcess[0]:
RequiredAssemblies[0]:
OtherFiles[0]:

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*
   3:  
   4:  
   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}
  19:  
  20:  
  21:  
  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
  29:  
  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 3 Comments »

Merry Christmas from the PowerShell Team!

http://blogs.msdn.com/powershell/

Selected New and Updated Features in Windows PowerShell V2 CTP3
(Please refer to Release Notes and Help topics for more details)

·  PowerShell Remoting

·  Windows PowerShell Integrated Scripting Environment (ISE)

·  63 New Cmdlets

·  Modules

·  Script Debugging

·  Advanced Functions

·  PowerShell Class API

Comments No Comments »

Modules in PowerShell, among other things, will allow code to be shared very easily within a community. One of the issues with scripts and dot sourcing right now is that we end up polluting our working session with a lot of variables and functions that we don’t really need. These are helper functions and helper variables that are used by larger functions.

Modules solves this problem. You can create a module (Just name a PS1 script to .PSM1) and specify which functions/variables you want to export.

One way to think about this if you have a C# background is public vs private variables or methods in a class. The exported members are analogous to public methods.

You do this with the export-modulemember cmdlet at the end of your module. Notice that I have four distinct function in my module, test.psm1. However, I am only exporting the functions binn and foo. The other two do not show up when I call get-command. Note that when I call bar, it does indeed have access to foo.

image

BTW, cat2 is an alias I use for Lee Holmes’ Syntax Highlighting script. Just wait til you see what else Modules can do, especially Module Manifests. With these we can set all kinds of pre-requisites for our scripts, versions, and a number of other things as well.

Comments No Comments »

PowerShell Sessions are what are used to connect to remote computers using PowerShell. There are a bunch of new Cmdlets that use the noun PSSession. One big change from CTP2 to CTP3 is that we are now using PSSessions rather than Runspaces. I think this is great, as most of the ops/admin folks I work with don’t have a clue what a runspace is or why they would need one to connect to a remote computer. Session seems to be a much better noun. I think its right on par with a telnet session or an RDP session. This makes a lot sense from an Admin’s point of view.

Anyway, the way we use PSSessions is through the invoke-command Cmdlet. At a very basic level, invoke-command takes two parameters, a ComputerName and a scriptblock to execute on that remote computer as shown in the example below

   1: PS C:\> invoke-command -computer win7andys -scriptblock {cat env:\computername}
   2: win7andys
   3: PS C:\>

I came across a situation where I needed a variable (lets call it $a) that I was using in my local PSSession to be available in my remote PSSession. Let’s try this and see what happens.

   1: PS C:\> "Hello $a"
   2: Hello Foo
   3: PS C:\> invoke-command -computer win7andys -scriptblock {"Hello $a"}
   4: Hello

Notice that $a was there locally but not when we tried to use it remotely in our scriptblock.

Lucky for us, there is a –ArgumentList parameter that we can use with invoke-command. This looks promising.

   1: PS C:\> invoke-command -computer win7andys -ArgumentList $a -scriptblock {"Hello $a"}
   2: Hello
   3: PS C:\>

Still no dice, but we are close. Turns out we need to declare $a as a parameter in our scriptblock.

   1: PS C:\> invoke-command -computer win7andys -Argu $a -scriptblock {param ($a) "Hello $a"}
   2: Hello Foo
   3: PS C:\>

And now we have it!

Comments 3 Comments »