none
Windows PowerShell 3.0: Cannot validate argument on parameter

    Question

  • I defined this function

    Function GetItem ($CurrentItem,$Number) {
    	Return $CurrentItem | Select-Object -Skip $Number -First 1
    }
    

    and then tried to run

    $Text = (GetItem($PWD,0)).FullName -Replace ([Regex]::Escape($Root)+"\\"),""

    and I got 

    Select-Object : Cannot validate argument on parameter 'Skip'. The argument is null, empty, or an element of the 
    argument collection contains a null value. Supply a collection that does not contain any null values and then try 
    the command again.
    At line:2 char:44
    +     Return $CurrentItem | Select-Object -Skip $Number -First 1
    +                                               ~~~~~~~
        + CategoryInfo          : InvalidData: (:) [Select-Object], ParameterBindingValidationException
        + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SelectObjectCommand
    
    

    Why I am I getting this error even though the argument passed to the function is clearly not a null value?

    Sunday, March 03, 2013 4:29 AM

Answers

  • Look closely at what you are doing here.

    $Text = (         GetItem($PWD,0)       ).FullName -Replace ([Regex]::Escape($Root)+"\\"),""

    You cannot call a function with parenthesis and commas.  This evaluates to an array of $pwd,0 or @($pwd,0) which is not what you want.

    The rest of what you are doing is not discernible and looks like you are trying to hard to do something  very simple.


    ¯\_(ツ)_/¯

    Sunday, March 03, 2013 7:11 AM
  • In Powershell, paremeters in function calls are seperated by space.
    Also you should not use parentheses when calling a function.
    So to sum up, you should call the function like this:

    GetItem $PWD 0


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog

    Sunday, March 03, 2013 6:23 PM

All replies

  • Does this give you different results?

    Function GetItem ($CurrentItem,$Number) {
    	Return ($CurrentItem | Select-Object -Skip $Number -First 1)
    }


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Sunday, March 03, 2013 5:40 AM
    Moderator
  • Look closely at what you are doing here.

    $Text = (         GetItem($PWD,0)       ).FullName -Replace ([Regex]::Escape($Root)+"\\"),""

    You cannot call a function with parenthesis and commas.  This evaluates to an array of $pwd,0 or @($pwd,0) which is not what you want.

    The rest of what you are doing is not discernible and looks like you are trying to hard to do something  very simple.


    ¯\_(ツ)_/¯

    Sunday, March 03, 2013 7:11 AM
  • How can I call a two-argument function, then? How can I get ($PWD).Count to return 0 if the present working directory is empty?
    • Edited by I am Melab Sunday, March 03, 2013 2:51 PM
    Sunday, March 03, 2013 2:19 PM
  • In Powershell, paremeters in function calls are seperated by space.
    Also you should not use parentheses when calling a function.
    So to sum up, you should call the function like this:

    GetItem $PWD 0


    Best Regards
    Claus Codam
    Consultant, Developer
    Coretech - Blog

    Sunday, March 03, 2013 6:23 PM