Proxy Function - extending through existing parameters

Hi Guys,

During my study with PowerShell I was seeking to extend the way the Invoke-Pester works to customize for my company.

I saw Don Jones (many thanks to you btw) earlier tutorials on how to extend existing cmdlets through the use of Proxy Functions. It was exactly what I was looking for.

The goal is to pass parameters to Invoke-Pester but have it shortened to Parameters. I saw June’s example of passing parameters to Pester using a hashtable and the script parameter. (URL link here)

I would like something like this:

Invoke-DCoastest -path ‘c:\test\server01.tests.ps1’ -computername ‘server01’ -ipaddress ‘192.168.1.200’ -primarydns ‘192.168.1.203’ -secondarydns ‘192.168.1.204’ -VMWareToolsVer ‘9.1234.123’ -timeserver ‘192.168.1.209’

I have given a shot and come up with the below:

$scriptCmd = {& $wrappedCmd @PSBoundParameters -Script @{ Path = $path; Parameters = @{ computername = $computername; ipaddress = $ipaddress; primarydns = $primarydns; secondarydns = $secondarydns; timeserver = $timeserver; VMwareToolsVer = $VMWareToolsVer } } }

However for some reason the way I am passing the parameters doesn’t look right.
I would appreciate some feedback to see if I have got it right.

Function Invoke-DCoatest{
    [CmdletBinding(DefaultParameterSetName = 'LegacyOutputXml')]
    param
    (
	    [Parameter(Position = 0)]
	    [Alias('Path', 'relative_path')]
	    [System.Object[]]$Script,
	    [Parameter(Position = 1)]
	    [Alias('Name')]
	    [string[]]$TestName,
	    [Parameter(Position = 2)]
	    [switch]$EnableExit,
	    [Parameter(ParameterSetName = 'LegacyOutputXml',
				      Position = 3)]
	    [string]$OutputXml,
	    [Parameter(Position = 4)]
	    [Alias('Tags')]
	    [string[]]$Tag,
	    [string[]]$ExcludeTag,
	    [switch]$PassThru,
	    [System.Object[]]$CodeCoverage,
	    [switch]$Strict,
	    [Parameter(ParameterSetName = 'NewOutputSet',
				      Mandatory = $true)]
	    [string]$OutputFile,
	    [Parameter(ParameterSetName = 'NewOutputSet')]
	    [ValidateSet('LegacyNUnitXml', 'NUnitXml')]
	    [string]$OutputFormat,
	    [switch]$Quiet,
	    [System.Object]$PesterOption,
	    [Parameter(Mandatory = $true)]
	    [string]$primarydns,
	    [Parameter(Mandatory = $true)]
	    [string]$secondarydns,
	    [Parameter(Mandatory = $true)]
	    [string]$VMWareToolsVer,
	    [Parameter(Mandatory = $true)]
	    [string]$ipaddress,
	    [Parameter(Mandatory = $true)]
	    [string]$computername,
	    [Parameter(Mandatory = $true)]
	    [string]$timeserver
    )

    begin
    {
        $_ipaddress    =$ipaddress
        $_computername =$computername
        $_primarydns   =$primarydns
        $_secondarydns =$secondarydns
        $_timeserver   =$timeserver
        $_VMWareToolsVer=$VMWareToolsVer

        $PSBoundParameters.Remove('ipaddress')
        $PSBoundParameters.Remove('computername')
        $PSBoundParameters.Remove('primarydns')
        $PSBoundParameters.Remove('secondarydns')
        $PSBoundParameters.Remove('timeserver')
        $PSBoundParameters.Remove('VMWareToolsVer')
        try {
            $outBuffer = $null
            if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer))
            {
                $PSBoundParameters['OutBuffer'] = 1
            }
            $wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Invoke-Pester', [System.Management.Automation.CommandTypes]::Function)
            $scriptCmd = {& $wrappedCmd @PSBoundParameters -Script @{ Path = $path; Parameters = @{ computername = $computername; ipaddress = $ipaddress; primarydns = $primarydns; secondarydns = $secondarydns; timeserver = $timeserver; VMwareToolsVer = $VMWareToolsVer } } }
		    $steppablePipeline = $scriptCmd.GetSteppablePipeline()
            $steppablePipeline.Begin($PSCmdlet)
        } catch {
            throw
        }
    }

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

    end
    {
        try {
            $steppablePipeline.End()
        } catch {
            throw
        }
    }
    
}

I’m not sure what you mean by “pass parameters to Invoke-Pester but have it shortened to Parameters” - can you maybe show an example of what you’d like to do, rather than just the code you’re trying? Without knowing the end goal I’m not sure I understand.

Thanks Don for the reply.

Well an example would be like this:

Invoke-DCoastest -path ‘c:\test\server01.tests.ps1’ -computername ‘server01’ -ipaddress ‘192.168.1.200’ -primarydns ‘192.168.1.203’ -secondarydns ‘192.168.1.204’ -VMWareToolsVer ‘9.1234.123’ -timeserver ‘192.168.1.209’

Invoke-DCoatest will be a proxy function of Invoke-Pester.

If what you’re doing works, I don’t see anything “wrong” with it just from a read-through. I mean… I’m not sure this is really the intended usage pattern for Pester. I mean, I’m not sure you need a proxy function, right? Why not just write a normal function that accepts some parameters, and then calls Invoke-Pester?

But whatever works for ya ;).