Proxy for import-module

I do quite a lot with modules as you can tell from my posts over the last few months and have bit a fair collection of modules. One issue I find when loading a module is that I can’t always remember the name of the commands that the module creates. If we use

Import-Module -Name filefunctions

we just get the prompt returned and no information on the commands. After a bit of experimenting I found that

Import-Module -Name filefunctions -PassThru | foreach {Get-Command -Module $_.Name | select name}

will give me the list of commands added by a particular module.  Instead of get-command I could have used

Get-Module filefunctions | select -ExpandProperty ExportedCommands

Rather than remembering to run the get-command I could have implemented a function that would run the commands. However, I thought it might be more fun to create a proxy command as detailed in these posts

http://blogs.msdn.com/powershell/archive/2009/01/04/extending-and-or-modifing-commands-with-proxies.aspx

http://blogs.msdn.com/powershell/archive/2009/01/05/ps-dir-a-d-the-screencast.aspx

I found the video in the second one irritating as it stopped every few seconds to ask if I wanted to continue watching.

Anyway we use the metaprogramming module that can be downloaded from the first post. This gives us a New-ProxyCommand function that we use like this.

New-ProxyCommand Import-Module -CommandType all -AddParameter ListMember > import-mymodule.ps1

The output script then has the code to execute the extra parameter inserted – the skeleton can be found in the block comment at the top of the script. To produce something like this

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
<# You are responsible for implementing the logic for added parameters. These parameters are bound to $PSBoundParameters so if you pass them on the the command you are proxying, it will almost certainly cause an error. This logic should be added to your BEGIN statement to remove any specified parameters from $PSBoundParameters. In general, the way you are going to implement additional parameters is by modifying the way you generate the $scriptCmd variable. Here is an example of how you would add a -SORTBY parameter to a cmdlet: ################################################################################ New ATTRIBUTES if ($ListMember) { [Void]$PSBoundParameters.Remove("ListMember") } ################################################################################ #>

[CmdletBinding(DefaultParameterSetName='Name')]
param(
    [Switch]
    ${Global},

    [ValidateNotNull()]
    [System.String]
    ${Prefix},

    [Parameter(ParameterSetName='Name', Mandatory=$true, Position=0, ValueFromPipeline=$true)]
    [System.String[]]
    ${Name},

    [Parameter(ParameterSetName='Assembly', Mandatory=$true, Position=0, ValueFromPipeline=$true)]
    [System.Reflection.Assembly[]]
    ${Assembly},

    [ValidateNotNull()]
    [System.String[]]
    ${Function},

    [ValidateNotNull()]
    [System.String[]]
    ${Cmdlet},

    [ValidateNotNull()]
    [System.String[]]
    ${Variable},

    [ValidateNotNull()]
    [System.String[]]
    ${Alias},

    [Switch]
    ${Force},

    [Switch]
    ${PassThru},

    [Switch]
    ${AsCustomObject},

    [Parameter(ParameterSetName='Name')]
    [System.Version]
    ${Version},

    [Parameter(ParameterSetName='ModuleInfo', Mandatory=$true, Position=0, ValueFromPipeline=$true)]
    [System.Management.Automation.PSModuleInfo[]]
    ${ModuleInfo},

    [Alias('Args')]
    [System.Object[]]
    ${ArgumentList},

    [Switch]
    ${DisableNameChecking},

    [Switch]
    ${ListMember})

begin
{
    try {
        $outBuffer = $null
        if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
        {
            $PSBoundParameters['OutBuffer'] = 1
        }
        $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Import-Module', [System.Management.Automation.CommandTypes]::Cmdlet)
        if ($ListMember)
        {
            [Void]$PSBoundParameters.Remove("ListMember")
            $scriptCmd = {& $wrappedCmd @PSBoundParameters | foreach {Get-Command -Module $_.Name | select name}}
        }else
        {
            $scriptCmd = {& $wrappedCmd @PSBoundParameters }
        }
        $steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin)
        $steppablePipeline.Begin($PSCmdlet)
    } catch {
        throw
    }
}

process
{
    try {
        $steppablePipeline.Process($_)
    } catch {
        throw
    }
}

end
{
    try {
        $steppablePipeline.End()
    } catch {
        throw
    }
}
<# .ForwardHelpTargetName Import-Module .ForwardHelpCategory Cmdlet #>

 

The important part is

if ($ListMember)
{
    [Void]$PSBoundParameters.Remove("ListMember")
    $scriptCmd = {& $wrappedCmd @PSBoundParameters | foreach {Get-Command -Module $_.Name | select name}}
}else
{
    $scriptCmd = {& $wrappedCmd @PSBoundParameters }
}

where we add the extra code we need for the new parameter.

We can use the new script in place of import-module

PS> .\import-mymodule.ps1 filefunctions
PS> .\import-mymodule.ps1 filefunctions -Force
PS> .\import-mymodule.ps1 filefunctions -Force -PassThru -ListMember

Name
----
Format-Colorfile
Format-Xml
Get-EmptyFolder
Get-TempContents
New-TempFile
Remove-EmptyFolder
Remove-TempContents
Remove-ZoneIdentifier
Test-ZoneIdentifier

 

It is a simple task to make this a function that is loaded in the profile.

This isn’t a technique for everyone but if you feel comfortable experimenting like this give it a try.  There must be a number of other cmdlets where we can modify the behaviour to add functionality to suit our individual needs.

Leave a Reply