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


  1.    25 Aug 2006 #1
    =?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

  2.    28 Aug 2006 #2
    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

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

Similar Threads
Thread Forum
test-path -IsValid ?
Hi, I have been playing with Powershell and I like it very much. I have a question about the "IsValid" switch of the "test-path" cmdlet. ...
PowerShell
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...
PowerShell
Test-Path isValid parameter
From the help file -isValid <SwitchParameter> Determines whether the syntax of the path is correct, regardless of whether the elements of the...
PowerShell
UNC Path - folder is empty
I am running Windows Vista Home Professional and using a Linksys WRT350N wireless router with the USB storage link. Attached to the router, I have...
Vista networking & sharing
how to test for a program in my path?
I'm building a test harness and would like to test if a utility is available before using it, in case a user of the framework hasn't installed it. ...
PowerShell
BUG/ANNOYANCE: PoSH autocompletes the full path rather than a minimal path
If you attempt tab completion on a file in a subdirectory of the current one (eg, sub<tab>\fi<tab> for subdir\file), PoSH autocompletes the full...
PowerShell
Binding(string path) - what is the syntax for the path?
Hi all, Continuing in my quest to build a custom control for a databound grid in WPF, I'm at the point where I'm trying to get each cell to...
Avalon

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    © Designer Media Ltd
    All times are GMT -5. The time now is 03:38.
    .