• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

  • Thread starter =?Utf-8?B?Um9tYW4gS3V6bWlu?=
  • Start date
?

=?Utf-8?B?Um9tYW4gS3V6bWlu?=

#1
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 Computer

J

James Truher

#2
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/scriptcenter/hubs/msh.mspx
"Roman Kuzmin" <[email protected]> wrote in message
news:[email protected]
>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 Computer