I am having some trouble validating a file input in a script. Using the following function
Function pathtest {
[CmdletBinding()]
param (
[ValidateScript({
if (-Not ($_ | Test-Path -PathType Leaf) ) {
throw "The Path argument must be a file. Folder paths are not allowed."
}
return $true
})]
[System.IO.FileInfo]$Path
)
Write-Host ("Output file: {0}" -f $Path.FullName)
}
Then I call the function with these two input files
pathtest -Path c:\temp\test.txt
pathtest -Path c:\temp\test.csv
The first one (test.txt) returns the path, but the second one (test.csv) returns an error:
PS C:\> pathtest c:\it\test.txt
Output file: c:\it\test.txt
PS C:\> pathtest c:\it\test.csv
pathtest : Cannot validate argument on parameter 'Path'. The Path argument must be a file. Folder paths are not
allowed.
At line:1 char:10
+ pathtest c:\it\test.csv
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [pathtest], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,pathtest
Any idea what's up with this?