Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[CTP3] CmdletBinding attribute syntax with named functions

437 views
Skip to first unread message

Joris van Lier

unread,
Dec 29, 2008, 9:37:11 AM12/29/08
to
What is the right syntax for using CmdletBinding attribute?

PS> get-help about_functions_advanced
...
get-help About_Functions_CmdletBindingAttribute
...

PS> [CmdletBinding] example-foo
Unexpected token 'example-foo' in expression or statement.
At line:1 char:28
+ [CmdletBinding] example-foo <<<<
+ CategoryInfo : ParserError: (example-foo:String) [],
ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken

Joris van Lier

unread,
Dec 29, 2008, 10:00:00 AM12/29/08
to
The following syntax can be parsed but I must be doing something wrong..

PS> function example()
>> {
>> [CmdletBinding]
>>
>> [Parameter(Mandatory=$true)]
>> [string] $server
>> [Parameter(Mandatory=$false)]
>> [string] $user
>> [Parameter(Mandatory=$true)]
>> [boolean] $enable
>> [Parameter(Mandatory=$false)]
>> [boolean] $verbose
>> Process
>> {
>> '& cscript "test.wsf" //Job:"example" @psBoundParameters'
>> }
>> }
>>
PS> example
Unable to find type [CmdletBinding]: make sure that the assembly containing
this type is loaded.
At line:3 char:20
+ [CmdletBinding] <<<<
+ CategoryInfo : InvalidOperation: (CmdletBinding:String) [],
RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

Shay Levy [MVP]

unread,
Dec 29, 2008, 5:21:49 PM12/29/08
to

[CmdletBinding()]

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

JL> The following syntax can be parsed but I must be doing something
JL> wrong..
JL>
PS>> function example()
PS>>

>>> {
>>> [CmdletBinding]
>>> [Parameter(Mandatory=$true)]
>>> [string] $server
>>> [Parameter(Mandatory=$false)]
>>> [string] $user
>>> [Parameter(Mandatory=$true)]
>>> [boolean] $enable
>>> [Parameter(Mandatory=$false)]
>>> [boolean] $verbose
>>> Process
>>> {
>>> '& cscript "test.wsf" //Job:"example" @psBoundParameters'
>>> }
>>> }
PS>> example

PS>>
JL> Unable to find type [CmdletBinding]: make sure that the assembly
JL> containing
JL> this type is loaded.
JL> At line:3 char:20
JL> + [CmdletBinding] <<<<
JL> + CategoryInfo : InvalidOperation:
JL> (CmdletBinding:String) [],
JL> RuntimeException
JL> + FullyQualifiedErrorId : TypeNotFound


Shay Levy [MVP]

unread,
Dec 29, 2008, 5:32:47 PM12/29/08
to
Syntax:

[CmdletBinding(SupportsShouldProcess=<Boolean>,
SupportsTransactions=<Boolean>,
ConfirmImpact=<String>,
DefaultParameterSetName=<String>)]


SupportsShouldProcess:
When set to $true, the function can support –confirm and –whatif

SupportsTransactions:
When set to $true, the function can support transactions

ConfirmImpact:
High, Medium, Low, or None

DefaultParameterSetName:
The name of the default parameter set.

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar

SL> [CmdletBinding()]
SL>
SL> ---
SL> Shay Levy
SL> Windows PowerShell MVP
SL> http://blogs.microsoft.co.il/blogs/ScriptFanatic
SL> PowerShell Toolbar: http://tinyurl.com/PSToolbar

Joris van Lier

unread,
Dec 30, 2008, 4:38:12 PM12/30/08
to
Thanks Shay, this is what I ended up with

function example() {
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$true)]
[string]$server,
[Parameter(Mandatory=$false)]
[string]$user,
[Parameter(Mandatory=$true)]
[boolean]$enable,
[Parameter(Mandatory=$false)]
[boolean]$verbose
)

PROCESS {
"..."
}
}

And I just found out overriding default parameters is a little more
complicated

PS> get-help example
Get-Help : A parameter with the name 'Verbose' was defined multiple times
for the command.
At line:1 char:9
+ get-help <<<< example
+ CategoryInfo : MetadataError: (:) [Get-Help],
MetadataException
+ FullyQualifiedErrorId :
ParameterNameAlreadyExistsForCommand,Microsoft.PowerShell.Commands.GetHelpCommand

"Shay Levy [MVP]" <n...@addre.ss> wrote in message
news:95d808935ed358...@news.microsoft.com...

Joel (Jaykul) Bennett

unread,
Dec 31, 2008, 12:02:32 AM12/31/08
to
You cannot (and should not try to) write your own implementation of
the "common parameters" ... you can check what they're set to and
change your behavior accordingly if you want to, but you can't replace
them, because they should be the same everywhere ;-)

--
Joel "Jaykul" Bennett
http://HuddledMasses.org

Thomas Lee

unread,
Jan 3, 2009, 6:28:02 PM1/3/09
to
In message <95d808935ed2b8...@news.microsoft.com>, Shay Levy
<?@addre.ss.invalid> writes
>
>[CmdletBinding()]

What does this attribute actually do?

--
Thomas Lee
doct...@gmail.com
MVP - Admin Frameworks and Security

Shay Levy [MVP]

unread,
Jan 4, 2009, 4:32:51 AM1/4/09
to

It gives you control over its named arguments:

SupportsShouldProcess
DefaultParameterSet
ConfirmImpact


See also these help files:

About_Functions_CmdletBinding
About_Functions_CmdletBindingAttribute

---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar


TL> In message <95d808935ed2b8...@news.microsoft.com>, Shay
TL> Levy <?@addre.ss.invalid> writes
TL>
>> [CmdletBinding()]
>>
TL> What does this attribute actually do?
TL>


0 new messages