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 - Test-Path '' -IsValid does not return False but fails. Is this OK?

Reply
 
Old 11-08-2007   #1 (permalink)
Roman Kuzmin


 
 

Test-Path '' -IsValid does not return False but fails. Is this OK?

Test-Path $Path -IsValid fails with ParameterBindingValidationException
(Test-Path : Cannot bind argument to parameter 'Path' because it is...) is
$Path is null or an empty string.

As a result one has to check validity of $Path in this way:
if ($Path -and (Test-Path $Path -IsValid)) {...}

Is this intentional?

--
Thanks,
Roman Kuzmin
PowerShellFar and FarNET: http://code.google.com/p/farnet/




My System SpecsSystem Spec
Old 11-08-2007   #2 (permalink)
Shay Levi


 
 

Re: Test-Path '' -IsValid does not return False but fails. Is this OK?

There is no default value for -path (as opposed to get-childitem which lists
the current directory), so it fails.
You need to validate $path prior to testing it. -isValid can only run if
the $path is not null or empty.

You can see it by running:

PS > test-path
cmdlet test-path at command pipeline position 1
Supply values for the following parameters:
Path[0]:
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty
array.
At line:1 char:9
+ test-path <<<<

Is it intensional? I think it is. Here's what the help says (it's required):

PARAMETERS
-path <string[]>
Specifies a path to be tested. Wildcards are permitted. If the path
includes spaces, enclose it in quotation marks. The parameter n
ame ("-Path") is optional.

Required? true
...



-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:

> Test-Path $Path -IsValid fails with
> ParameterBindingValidationException (Test-Path : Cannot bind argument
> to parameter 'Path' because it is...) is $Path is null or an empty
> string.
>
> As a result one has to check validity of $Path in this way: if ($Path
> -and (Test-Path $Path -IsValid)) {...}
>
> Is this intentional?
>

My System SpecsSystem Spec
Old 11-08-2007   #3 (permalink)
Roman Kuzmin


 
 

Re: Test-Path '' -IsValid does not return False but fails. Is this OK?

> Is it intensional? I think it is. Here's what the help says (it's
Quote:

> required):
Perhaps you are talking about something else. Empty string or null
parameters are not the same as omitted parameters.

$Path = ''
Test-Path -Path $Path -IsValid

Output:
Test-Path : Cannot bind argument to parameter 'Path' because it is an empty
string.
....

Note, I do provide parameter -Path, so that 'required' is satisfied. But I
rather expect

Output:
False

than a failure message in this case.

--
Thanks,
Roman Kuzmin
PowerShellFar and FarNET: http://code.google.com/p/farnet/


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


 
 

Re: Test-Path '' -IsValid does not return False but fails. Is this OK?

I can see what you mean.

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com


Quote:
Quote:

>> Is it intensional? I think it is. Here's what the help says (it's
>> required):
>>
> Perhaps you are talking about something else. Empty string or null
> parameters are not the same as omitted parameters.
>
> $Path = ''
> Test-Path -Path $Path -IsValid
> Output:
> Test-Path : Cannot bind argument to parameter 'Path' because it is an
> empty
> string.
> ...
> Note, I do provide parameter -Path, so that 'required' is satisfied.
> But I rather expect
>
> Output:
> False
> than a failure message in this case.
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
HELP! fileExist function always return 'FALSE' VB Script
RegEx to return False if input is invalid. VB Script
test-path -IsValid ? PowerShell
Test-Path isValid parameter PowerShell
BUG? (Test-Path $path -IsValid) and empty $path 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