When a parameter is not supplied to my script, I want to include the parameter/variable name as a string in the Write-Error
message:
$RequiredArgs = @($Arg1, $Arg2, $Arg3, $Arg4)
foreach ($Arg in $RequiredArgs) {
if (!($Arg)) {
Write-Error "Argument: $Arg is required."
throw
}
}
This currently outputs Argument: is required.
. I have tried quoting the variable name:
Write-Error "Argument: "$Arg" is required."
But this shows the error: Write-Error: A positional parameter cannot be found that accepts argument ' is required.
Is there a way to output the variable name, even if the parameter is not set?