I need to write a function which defines a bool Parameter automatically with [ValidateScript()].
function Deploy-App {
Param(
[Parameter(Position=0)]
[ValidateScript({if (Test-Path .\DeployFiles.txt) { $UseFilepathFile = $true }})]
[Alias("u")]
[bool]$UseFilepathFile
)
Get-Location
Write-Host $UseFilepathFile
}
Why does this always return $false even though the file exists in the current location? Is the usage of ValidateScript() wrong and I can't use it like this? How else would I tackle my problem?
ValidateScript()is for validating values passed into a function, not for assigning values to variables. The latter would go into the function bod, e.g.$UseFilepathFile = Test-Path .\DeployFiles.txt. Please take a step back and describe the actual problem you're trying to solve. What do you need this for?