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 Tutorial - BUG? (Test-Path $path -IsValid) and empty $path

Reply
 
Old 08-25-2006   #1 (permalink)
=?Utf-8?B?Um9tYW4gS3V6bWlu?=
Guest


 
 

BUG? (Test-Path $path -IsValid) and empty $path

I would like to check if $path is a syntactically valid path using Test-Path
with -IsValid switch , i.e.:

PS> $path = '' # why not?
PS> if (!(Test-Path $path -IsValid)) {Write-Warning "Path <$path> is
invalid"}

Surprisingly Test-Path fails and instead of expected

WARNING: Path <> is invalid

I get very disappointing:

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

The problem is not just a misleading message, but something worse - my code
was not executed as it was designed for an invalid path.

Note: I can’t even suppress this failure:

PS> Test-Path '' -ea SilentlyContinue
Test-Path : Cannot bind argument to parameter 'Path' because it is an
empty string.

Then, if -Path is an empty string, there are even more oddities:

PS> Test-Path '' -xxx

Instead of more likely expected error

Test-Path : A parameter cannot be found that matches parameter name 'xxx'.

I get again

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

All this does not look good, IMO.
Any ideas? Am I missing something?

--
Thanks,
Roman

My System SpecsSystem Spec
Old 08-28-2006   #2 (permalink)
James Truher
Guest


 
 

Re: BUG? (Test-Path $path -IsValid) and empty $path

This does look like a bug, and happening in the parameter binding code, i
believe. Also, unfortunately is seems that all of the *path cmdlets suffer
from this. There is a way around this with a trap

if ( ! $( trap { continue }; test-path $path -isvalid)) { Write-Warning
"Path <$path> is invalid }

I haven't looked at the code, but my guess is that the ErrorAction parameter
value is not applied to the parameter binding process, thus you don't get
the ErrorAction behavior that you specified.

jim

--
James Truher[MSFT]
Program Manager - Windows PowerShell
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx
"Roman Kuzmin" <RomanKuzmin@discussions.microsoft.com> wrote in message
news:E6A307BF-CCE8-4306-844E-4E724DF61EAD@microsoft.com...
>I would like to check if $path is a syntactically valid path using
>Test-Path
> with -IsValid switch , i.e.:
>
> PS> $path = '' # why not?
> PS> if (!(Test-Path $path -IsValid)) {Write-Warning "Path <$path> is
> invalid"}
>
> Surprisingly Test-Path fails and instead of expected
>
> WARNING: Path <> is invalid
>
> I get very disappointing:
>
> Test-Path : Cannot bind argument to parameter 'Path' because it is an
> empty string.
>
> The problem is not just a misleading message, but something worse - my
> code
> was not executed as it was designed for an invalid path.
>
> Note: I can't even suppress this failure:
>
> PS> Test-Path '' -ea SilentlyContinue
> Test-Path : Cannot bind argument to parameter 'Path' because it is an
> empty string.
>
> Then, if -Path is an empty string, there are even more oddities:
>
> PS> Test-Path '' -xxx
>
> Instead of more likely expected error
>
> Test-Path : A parameter cannot be found that matches parameter name
> 'xxx'.
>
> I get again
>
> Test-Path : Cannot bind argument to parameter 'Path' because it is an
> empty string.
>
> All this does not look good, IMO.
> Any ideas? Am I missing something?
>
> --
> Thanks,
> Roman



My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
test-path -IsValid ? PowerShell
Test-Path '' -IsValid does not return False but fails. Is this OK? PowerShell
Test-Path isValid parameter PowerShell
BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal 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