ARG!? and I mean that literally...


what need way validate parameter such function can assume

parameter of particular type, in case, should valid powershell command of description.

first try:

ps c:\>     function zap {  >>     param (  >>         [validatescript({get-command $_})]  >>         $cmd  >>     )  >>         if ( $cmd -eq '' ) {'parameter blank'}  >>         if ( $cmd -eq $null) {'parameter null'}  >>         write-host "this command: '$cmd'"  >>     }  >>  ps c:\>     zap zap    # 1. should work  command: 'zap'  ps c:\>     zap zappa  # 2. should fail  zap : cannot validate argument on parameter 'cmd'. "get-command $_" validation script   argument value "  zappa" did not return true. determine why validation script failed , try command   again.  @ line:1 char:8  +     zap <<<<  zappa  # 2. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap ''     # 3. should fail  zap : cannot validate argument on parameter 'cmd'. cannot validate argument on parameter   'name'. argument null o  r empty. supply argument not null or empty , try command again.  @ line:1 char:8  +     zap <<<<  ''     # 3. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap $null  # 4. should fail  zap : cannot validate argument on parameter 'cmd'. argument null, empty, or element   of argument collectio  n contains null value. supply collection not contain null values , try   command again.  @ line:1 char:8  +     zap <<<<  $null  # 4. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap        # 5. should fail  parameter null  command: ''  ps c:\>

i have thought parameter no content have failed get-command validation, apparently not. of course if $_ blank get-command succeed, because without parameters returns list of commands. not case command:

    get-command ""

so here's second attempt:

ps c:\>     function zap {  >>     param (  >>         [validatescript({get-command "$_"})]  >>         $cmd  >>     )  >>         if ( $cmd -eq '' ) {'parameter blank'}  >>         if ( $cmd -eq $null) {'parameter null'}  >>         write-host "this command: '$cmd'"  >>     }  >>  ps c:\>     zap zap    # 1. should work  command: 'zap'  ps c:\>     zap zappa  # 2. should fail  zap : cannot validate argument on parameter 'cmd'. "get-command "$_"" validation script   argument value   "zappa" did not return true. determine why validation script failed , try   command again.  @ line:1 char:8  +     zap <<<<  zappa  # 2. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap ''     # 3. should fail  zap : cannot validate argument on parameter 'cmd'. cannot validate argument on parameter   'name'. argument null o  r empty. supply argument not null or empty , try command again.  @ line:1 char:8  +     zap <<<<  ''     # 3. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap $null  # 4. should fail  zap : cannot validate argument on parameter 'cmd'. argument null, empty, or element   of argument collectio  n contains null value. supply collection not contain null values , try   command again.  @ line:1 char:8  +     zap <<<<  $null  # 4. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap        # 5. should fail  parameter null  command: ''  ps c:\>

but still no luck. let's beef validation code in attempt three:

ps c:\>     function zap {  >>     param (  >>         [validatescript({get-command "$_"})]  >>         [validatenotnullorempty()]  >>         $cmd  >>     )  >>         if ( $cmd -eq '' ) {'parameter blank'}  >>         if ( $cmd -eq $null) {'parameter null'}  >>         write-host "this command: '$cmd'"  >>     }  >>  ps c:\>     zap zap    # 1. should work  command: 'zap'  ps c:\>     zap zappa  # 2. should fail  zap : cannot validate argument on parameter 'cmd'. "get-command "$_"" validation script   argument value   "zappa" did not return true. determine why validation script failed , try   command again.  @ line:1 char:8  +     zap <<<<  zappa  # 2. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap ''     # 3. should fail  zap : cannot validate argument on parameter 'cmd'. argument null or empty. supply   argument not null o  r empty , try command again.  @ line:1 char:8  +     zap <<<<  ''     # 3. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap $null  # 4. should fail  zap : cannot validate argument on parameter 'cmd'. argument null or empty. supply   argument not null o  r empty , try command again.  @ line:1 char:8  +     zap <<<<  $null  # 4. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap        # 5. should fail  parameter null  command: ''  ps c:\>

and still no luck. thing find odd validatenotnullorempty not flag missing parameter, while code in function seems detect condition $null.

so attempt 4 make mandatory parameter. surely work:

ps c:\>     function zap {  >>     param (  >>         [parameter(mandatory = $true)]  >>         [validatescript({get-command "$_"})]  >>         [validatenotnullorempty()]  >>         $cmd  >>     )  >>         if ( $cmd -eq '' ) {'parameter blank'}  >>         if ( $cmd -eq $null) {'parameter null'}  >>         write-host "this command: '$cmd'"  >>     }  >>  ps c:\>     zap zap    # 1. should work  command: 'zap'  ps c:\>     zap zappa  # 2. should fail  zap : cannot validate argument on parameter 'cmd'. "get-command "$_"" validation script   argument value   "zappa" did not return true. determine why validation script failed , try   command again.  @ line:1 char:8  +     zap <<<<  zappa  # 2. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap ''     # 3. should fail  zap : cannot validate argument on parameter 'cmd'. argument null or empty. supply   argument not null o  r empty , try command again.  @ line:1 char:8  +     zap <<<<  ''     # 3. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap $null  # 4. should fail  zap : cannot validate argument on parameter 'cmd'. argument null or empty. supply   argument not null o  r empty , try command again.  @ line:1 char:8  +     zap <<<<  $null  # 4. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap        # 5. should fail  cmdlet zap @ command pipeline position 1  supply values following parameters:  cmd:  zap : cannot validate argument on parameter 'cmd'. cannot validate argument on parameter   'name'. argument null o  r empty. supply argument not null or empty , try command again.  @ line:1 char:8  +     zap <<<<         # 5. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>

well n

ow we're getting somewhere. instead of reporting error when mandatory parameter absent, prompts user value use. that's fine interactive command. function going used in script run user not have better idea give parameter writer of script (me).

so, resolve this, seems must include code in function detect absent parameter comparing $null, though have used validatenotnullorempty. fortunately, not need create own version of error message see, can call function recursively so:

ps c:\>     function zap {  >>     param (  >>         [validatescript({get-command "$_"})]  >>         [validatenotnullorempty()]  >>         $cmd  >>     )  >>         if ( $cmd -eq $null) {zap $null;return}  >>         write-host "this command: '$cmd'"  >>     }  >>  ps c:\>     zap zap    # 1. should work  command: 'zap'  ps c:\>     zap zappa  # 2. should fail  zap : cannot validate argument on parameter 'cmd'. "get-command "$_"" validation script   argument value   "zappa" did not return true. determine why validation script failed , try   command again.  @ line:1 char:8  +     zap <<<<  zappa  # 2. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap ''     # 3. should fail  zap : cannot validate argument on parameter 'cmd'. argument null or empty. supply   argument not null o  r empty , try command again.  @ line:1 char:8  +     zap <<<<  ''     # 3. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap $null  # 4. should fail  zap : cannot validate argument on parameter 'cmd'. argument null or empty. supply   argument not null o  r empty , try command again.  @ line:1 char:8  +     zap <<<<  $null  # 4. should fail      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>     zap        # 5. should fail  zap : cannot validate argument on parameter 'cmd'. argument null or empty. supply   argument not null o  r empty , try command again.  @ line:7 char:34  +         if ( $cmd -eq $null) {zap <<<<  $null;return}      + categoryinfo          : invaliddata: (:) [zap], parameterbindingvalidationexception      + fullyqualifiederrorid : parameterargumentvalidationerror,zap  ps c:\>

this works, unfortunately, reports location of error "zap $null" statement in function instead of function called from.

can suggest way use validation (instead of coding) ensure non-mandatory parameter not missing? or, alternately, make mandatory such absense throw error instead of going interactive on me?


al dunbar

it may moot far usage. figure out how things work.  i'm more remember need use syntax if understand it's doing. 

 

before write off "syntactic coincidence" consider http://technet.microsoft.com/en-us/library/dd347600.aspx

validatescript validation attribute

        validatescript attribute specifies script used
        validate parameter value. windows powershell pipes
        parameter value script , generates error if the
        script returns "false" or if script throws exception.

        when use validatescript attribute, parameter value
        being validated mapped $_ variable. can
        use $_ variable refer parameter value in script.
       
        in following example, value of eventdate parameter
        must greater or equal current date.

        param
          (
            [parameter()]
            [validatescript({$_ -ge (get-date)})]
            [datetime]
            $eventdate
          )


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



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Motherboard replacement

Cannot create Full Text Search catalog after upgrading to V12 - Database is not fully started up or it is not in an ONLINE state

Remote Desktop App - Error 0x207 or 0x607