Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - CTP: Script cmdlet parameter attributes error

Reply
 
Old 12-11-2007   #1 (permalink)
Shay Levi


 
 

CTP: Script cmdlet parameter attributes error

This is a sample v2 script cmdlet that takes a string array for the "Name"
parameter.


Cmdlet Test-Param{

param(
[Mandatory]
[Position(0)]
[ValidateNotNullOrEmpty]
[ValueFromPipeline]
[string[]]$Name
)

process{
$Name | foreach { write-output $_ }
}


}


All is fine when I run:

Test-Param -name "shay","levi"

PS > shay
PS > levi


I get an error when I try to pipe the strings:

"shay","levi" | Test-Param


PS > shay
Attribute cannot be added because it would cause the variable Name with value
to become invalid.
+ <<<< "shay","levi" | Test-Param


When I remove the [ValidateNotNullOrEmpty] attribute, it works fine:

PS > shay
PS > levi



What am I doing wrong?


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



My System SpecsSystem Spec
Old 12-11-2007   #2 (permalink)
Jon


 
 

Re: Script cmdlet parameter attributes error

Giving it a default value seems to remove the error eg......

Cmdlet Test-Param{

param(
[Mandatory]
[Position(0)]
[ValidateNotNullOrEmpty]
[ValueFromPipeline]
[string[]]$Name="whatever"
)

process{
$Name | foreach { write-output $_ }
}


}


--
Jon


"Shay Levi" <no@xxxxxx> wrote in message
news:8766a94412b9b8ca0a25978bc204@xxxxxx
Quote:

> This is a sample v2 script cmdlet that takes a string array for the "Name"
> parameter.
>
> Cmdlet Test-Param{
>
> param(
> [Mandatory] [Position(0)]
> [ValidateNotNullOrEmpty]
> [ValueFromPipeline]
> [string[]]$Name
> )
>
> process{
> $Name | foreach { write-output $_ } }
>
>
> }
>
>
> All is fine when I run:
>
> Test-Param -name "shay","levi"
>
> PS > shay
> PS > levi
>
>
> I get an error when I try to pipe the strings:
>
> "shay","levi" | Test-Param
>
>
> PS > shay
> Attribute cannot be added because it would cause the variable Name with
> value to become invalid.
> + <<<< "shay","levi" | Test-Param
>
>
> When I remove the [ValidateNotNullOrEmpty] attribute, it works fine:
>
> PS > shay
> PS > levi
>
>
>
> What am I doing wrong?
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
My System SpecsSystem Spec
Old 12-11-2007   #3 (permalink)
Kiron


 
 

Re: CTP: Script cmdlet parameter attributes error

Try this also:

$strArray1 = 'one is element [0]','two is element [1]'
$strArray2 = 'three is element [0]','four is element [1]'

# unless you pipe a single element
'one is one' | test-param

# ...use the comma operator to ensure passing an Array
, $strArray1 | test-param
, ('one is element [0]','two is element [1]') | test-param
, [string[]]('one is element [0]','two is element [1]') | test-param

# array of arrays
, (('one is element [0][0]','two is element [0][1]'),
('three is element [1][0]','four is element [1][1]')) | test-param

# if not, only the first element is piped and of course that errors
$strArray1 | test-param

--
Kiron
My System SpecsSystem Spec
Old 12-11-2007   #4 (permalink)
Shay Levi


 
 

Re: CTP: Script cmdlet parameter attributes error

Thanks Kiron & Jon.

What If I pipe a file content:

PS > get-content names.txt | test-param

This gives the same error and I don't want to use a default value for the
name parameter.
A workaround is to omit the ValidateNotNullOrEmpty attribute but I wonder
why, in this case, both ValidateNotNullOrEmpty
and ValueFromPipeline can't live together.



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> This is a sample v2 script cmdlet that takes a string array for the
> "Name" parameter.
>
> Cmdlet Test-Param{
>
> param(
> [Mandatory]
> [Position(0)]
> [ValidateNotNullOrEmpty]
> [ValueFromPipeline]
> [string[]]$Name
> )
> process{
> $Name | foreach { write-output $_ }
> }
> }
>
> All is fine when I run:
>
> Test-Param -name "shay","levi"
>
PS>> shay
PS>> levi
Quote:

> I get an error when I try to pipe the strings:
>
> "shay","levi" | Test-Param
>
PS>> shay
PS>>
Quote:

> Attribute cannot be added because it would cause the variable Name
> with value
> to become invalid.
> + <<<< "shay","levi" | Test-Param
> When I remove the [ValidateNotNullOrEmpty] attribute, it works fine:
>
PS>> shay
PS>> levi
Quote:

> What am I doing wrong?
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic

My System SpecsSystem Spec
Old 12-11-2007   #5 (permalink)
Jon


 
 

Re: CTP: Script cmdlet parameter attributes error

The way I understand it, the 'mandatory' attribute already implies that it
can't be null, an empty string, or an empty array. So the additional
'ValidateNotNullOrEmpty' is redundant, since it's been previously implied.
You'd only use this attribute when you haven't specified Mandatory. You can
see that with the various errors here (remove the Mandatory and there would
be no errors) .....


#----------------------------------------------
Cmdlet Test-Param{

param(
[Mandatory]
[Position(0)]
[ValueFromPipeline]
[string[]]
$Name
)

process{
$Name | foreach { write-output $_ }
}

}

,@() | Test-Param
"jon","" | Test-Param
"jon", $null | Test-Param
"jon1", "jon2" | Test-Param

#-----------------------------------------------------------




--
Jon


"Shay Levi" <no@xxxxxx> wrote in message
news:8766a94412c9f8ca0a442f55e22c@xxxxxx
Quote:

> Thanks Kiron & Jon.
>
> What If I pipe a file content:
>
> PS > get-content names.txt | test-param
>
> This gives the same error and I don't want to use a default value for the
> name parameter.
> A workaround is to omit the ValidateNotNullOrEmpty attribute but I wonder
> why, in this case, both ValidateNotNullOrEmpty and ValueFromPipeline can't
> live together.
>
>
>
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
>
Quote:

>> This is a sample v2 script cmdlet that takes a string array for the
>> "Name" parameter.
>>
>> Cmdlet Test-Param{
>>
>> param(
>> [Mandatory]
>> [Position(0)]
>> [ValidateNotNullOrEmpty]
>> [ValueFromPipeline]
>> [string[]]$Name
>> )
>> process{
>> $Name | foreach { write-output $_ }
>> }
>> }
>>
>> All is fine when I run:
>>
>> Test-Param -name "shay","levi"
>>
> PS>> shay
> PS>> levi
Quote:

>> I get an error when I try to pipe the strings:
>>
>> "shay","levi" | Test-Param
>>
> PS>> shay
> PS>>
Quote:

>> Attribute cannot be added because it would cause the variable Name
>> with value
>> to become invalid.
>> + <<<< "shay","levi" | Test-Param
>> When I remove the [ValidateNotNullOrEmpty] attribute, it works fine:
>>
> PS>> shay
> PS>> levi
Quote:

>> What am I doing wrong?
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
>
>
My System SpecsSystem Spec
Old 12-11-2007   #6 (permalink)
Kiron


 
 

Re: CTP: Script cmdlet parameter attributes error

In that case you can:

, (get-content data.csv) | test-param

# ...or pipe each line to Test-Param
# but I don't think that's what you're after
get-content data.csv | % {$_ | Test-Param}

The problem is that when assigning by Pipeline Value, only the first element is assigned to $Name, unless you specifically pipe it as an array or just pipe one element instead.
Jon makes a good point. I used the functions below to list parameters with certain attributes and found none that had both [Mandatory] and [ValidateNotNullOrEmpty] and accepts [String[]] as Input, so like you say they can't live together

# These functions may not work in v1, not sure

function List-ParamsA {
# List-ParamsA lists parameter with the following attributes:
# [Mandatory][ValueFromPipeline][String[]]
# found 27
get-command * -type Cmdlet | % {
$Cmdlet = $_.Name
$_.ParameterSets | % {
$_.Parameters | ? {
$_.IsMandatory -and
$_.ValueFromPipeline -and
$_.ParameterType -match 'string\[]'
} | % {
"Cmdlet $Cmdlet > Parameter $($_.name)"
}
}
} | sort -unique
}

function List-ParamsB {
# List-ParamsB lists parameter with the following attributes:
# [ValueFromPipeline][ValidateNotNullOrEmpty][String[]]
# found 103
get-command * -type Cmdlet | % {
$Cmdlet = $_.Name
$_.ParameterSets | % {
$_.Parameters | ? {
$_.ValueFromPipeline -and
$_.ParameterType -match 'string\[]'
$_.Attributes -match 'ValidateNotNullOrEmpty'
} | % {
"Cmdlet $Cmdlet > Parameter $($_.name)"
}
}
} | sort -unique
}

function List-ParamsC {
# List-ParamsC lists parameter with the following attributes:
# [Mandatory][ValueFromPipeline][ValidateNotNullOrEmpty][String[]]
# found 0
get-command * -type Cmdlet | % {
$Cmdlet = $_.Name
$_.ParameterSets | % {
$_.Parameters | ? {
$_.IsMandatory -and
$_.ValueFromPipeline -and
$_.ParameterType -match 'string\[]' -and
$_.Attributes -match 'ValidateNotNullOrEmpty'
} | % {
"Cmdlet $Cmdlet > Parameter $($_.name)"
}
}
} | sort -unique
}

--
Kiron
My System SpecsSystem Spec
Old 12-12-2007   #7 (permalink)
Shay Levi


 
 

Re: CTP: Script cmdlet parameter attributes error

Thanks again guys!

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Thanks Kiron & Jon.
>
> What If I pipe a file content:
>
PS>> get-content names.txt | test-param
PS>>
Quote:

> This gives the same error and I don't want to use a default value for
> the
> name parameter.
> A workaround is to omit the ValidateNotNullOrEmpty attribute but I
> wonder
> why, in this case, both ValidateNotNullOrEmpty
> and ValueFromPipeline can't live together.
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>> This is a sample v2 script cmdlet that takes a string array for the
>> "Name" parameter.
>>
>> Cmdlet Test-Param{
>>
>> param(
>> [Mandatory]
>> [Position(0)]
>> [ValidateNotNullOrEmpty]
>> [ValueFromPipeline]
>> [string[]]$Name
>> )
>> process{
>> $Name | foreach { write-output $_ }
>> }
>> }
>> All is fine when I run:
>>
>> Test-Param -name "shay","levi"
>>
PS>>> shay
PS>>> levi
Quote:
Quote:

>> I get an error when I try to pipe the strings:
>>
>> "shay","levi" | Test-Param
>>
PS>>> shay
PS>>>
Quote:
Quote:

>> Attribute cannot be added because it would cause the variable Name
>> with value
>> to become invalid.
>> + <<<< "shay","levi" | Test-Param
>> When I remove the [ValidateNotNullOrEmpty] attribute, it works fine:
PS>>> shay
PS>>> levi
Quote:
Quote:

>> What am I doing wrong?
>>
>> -----
>> Shay Levi
>> $cript Fanatic
>> http://scriptolog.blogspot.com
>> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Cmdlet and parameter sets PowerShell
cmdlet parameter sets PowerShell
How to best control parameter attributes and parameter parsing in your own scripts? PowerShell
Cmdlet Parameter Positon PowerShell
small bug: awkward cmdlet error messages when no parameter is prov PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46