![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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>> levi Quote: > I get an error when I try to pipe the strings: > > "shay","levi" | Test-Param > 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>> 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 Specs![]() |
| | #5 (permalink) |
| | 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>> levi Quote: >> I get an error when I try to pipe the strings: >> >> "shay","levi" | Test-Param >> > 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>> 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
| | #7 (permalink) |
| | 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>> 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>>> levi Quote: Quote: >> I get an error when I try to pipe the strings: >> >> "shay","levi" | Test-Param >> 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>>> 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 Specs![]() |
![]() |
| 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 | |||