Declaring a -Force parameter in a cmdlet wrapper for purposes of mocking

31 views
Skip to first unread message

Jamie Thomson

unread,
Sep 17, 2014, 11:16:36 AM9/17/14
to pes...@googlegroups.com
Hi,
I have a question that is ostensibly about Powershell generally rather than specifically about Pester however its come about in the context of my using Pester Mocks so I'm hoping I'm not out of order posting this here.

I am testing a function (MyFunc) that makes a call to cmdlet Remove-AzureAutomationRunbook. In order that I can test MyFunc I have introduced a function Remove-AzureAutomationRunbookWrap that wraps the call to Remove-AzureAutomationRunbook in order that it is nice and easy to mock. Here's the definition of Remove-AzureAutomationRunbookWrap:

function Remove-AzureAutomationRunbookWrap {
 
[CmdletBinding()]
 
Param([Parameter(Mandatory=$true)][string]$AutomationAccountName,[Parameter(Mandatory=$true)][string]$Name)
 
New-AzureAutomationRunbook -AutomationAccountName $AutomationAccountName -Name $Name
}

Very simple.

Now, in MyFunc I was using the -Force parameter of Remove-AzureAutomationRunbook:

Remove-AzureAutomationRunbookWrap -AutomationAccountName $NLSAzureAutomationAccountName -Name $runbookName -Force


so I need to have a -Force parameter on Remove-AzureAutomationRunbookWrap too. Thing is, I don't know how to declare this parameter nor what value it will take given that no value is actually supplied to it. I've looked at the help for that parameter but the only really useful information is that that is designated a [<SwitchParameter>]. Can anyone tell me how I should declare my -Force parameter and also how I can pass on the whatever value it takes onto RemoveAzureAutomationRunbook.

Hope that makes sense. As I say this is a question about Powershell more than Pester, and I suspect its considered Powershell 101 stuff as well, however if anyone here can help I'd be very grateful.

cheers
Jamie

Dave Wyatt

unread,
Sep 17, 2014, 4:28:10 PM9/17/14
to pes...@googlegroups.com
Hi Jamie,

Those are called switch parameters, and there's a type accelerator for that:  [switch] $Force

Normally, when you call a command with a switch parameter, you just treat it as true if present and false if not.  However, as you've pointed out, on occasion you want to pass in a value based on some other variable, and there are a couple of ways you could do that here.  The most general-purpose answer is to place a colon between the name of the switch and the value you want to pass in:

Remove-AzureAutomationRunbook -Force:$Force

Since the parameter names in your wrap function exactly match those in the cmdlet, you could also just splat in the $PSBoundParameters table, and it would take care of the rest for you:

Remove-AzureAutomationRunbook @PSBoundParameters

You can find more information on that topic in the about_Splatting help file.

Dave

Jamie Thomson

unread,
Sep 18, 2014, 4:11:24 AM9/18/14
to pes...@googlegroups.com
Hi Dave,
Splatting and switch parameters eh. Love learning new stuff and stuff like this (especially @PSBoundParameters) is causing me to fall more in love with POSH every day. Through reading about splatting I learned about @Args too which also sounds ridiculously useful.

Thanks very very much for the help.
Reply all
Reply to author
Forward
0 new messages