Powershell :- Test-Path and the -IsValid Option

Background

In some cases one rushes through code and adds this and that in hopes to quickly get results.

And, that is how I found myself using Test-Path and self sabotaging with the -IsValid Option.

Script

Powershell

Code



Set-StrictMode -Version 1.0

Function fileValidation( `
                              [string]  $file  `
                            , [Boolean] $haltOnFailure
                            , [Boolean] $useIsValidFlag
                       )

{

    [boolean] $fileExist = $false;
    [boolean] $validationFailed = $false;
    [string]  $log="";
    [string]  $logBuffer="";

    [string]  $formatFileFailure="File ({0}) does not exist!. useIsValidFlag => {1} ";
    [string]  $formatFileSuccess="File ({0}) exists. useIsValidFlag => {1} ";

    #Set variables
    $validationFailed = $false;
    $fileExist = $false;

    if ($useIsValidFlag -eq $false)
    {

        $fileExist = Test-Path $file -PathType Leaf 

    }
    elseif ($useIsValidFlag -eq $true)
    {

        $fileExist = Test-Path $file -PathType Leaf -IsValid

    }   

    if ($fileExist -eq $False)
    {
        $validationFailed = $true;

        $log = $formatFileFailure -f $file, $useIsValidFlag

        $logBuffer = "{0}{1}`r`n" -f $logBuffer, $log;

        Write-Warning $log;

    }
    else
    {

        $log = $formatFileSuccess -f $file, $useIsValidFlag

        Write-Host $log;

    }

    if ($validationFailed -eq $true)
    {
        if ($haltOnFailure -eq $True)
        {

            #throw $logBuffer;

            [Environment]::Exit(1)
        }
    }       

    return ($validationFailed);

}

[boolean] $fileExist = $false;
[string]  $file="";
[boolean] $useIsValidFlag = $false;

$file="b:";
$file="c:\sam_was_here.txt";

Write-Host "";

$useIsValidFlag = $true;
$fileExist = fileValidation $file $true $useIsValidFlag;

Write-Host "";

$useIsValidFlag = $false;
$fileExist = fileValidation $file $true $useIsValidFlag;

Write-Host ""; 

Output

 fileExistence_IsValid_01_20200206_0851AM

Explanation

  1. In our test, we passed along a file that does not exist (c:\sam_was_here.txt)
    • -IsValid option
      • When Test-Path is called with the -IsValid option it returns a true
      • The reason is that the file existence is not actually tested
      • All that is tested is whether the file passed is syntactically correct

Source Code

GitHub

Gist

DanielAdeniji/fileExistenceCheck.ps1
Link

 

Summary

Test-Path -IsValid option is most appropriate when file names are received and one is simply trying to determine whether the received test could possibly be used as a valid file name.

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s