I would like the second function call in this script to throw an error:
function Deploy
{
param(
[Parameter(Mandatory=$true)]
[ValidateNotNullOrEmp
@Emperor XLII has a nice comment in the question that I think can be a better answer for some use cases:
if you run
powershell.exe
with the-NonInteractive
flag, missing mandatory parameters will cause an error and result in a non-zero exit code for the process.
The reasons to use this can be:
Mandatory=$true
parameters and the cost is high to convert all of them.Once the parameter is marked as mandatory PowerShell will prompt for value. That said, if you remove the mandatory attribute then you can set a default value with a throw statement:
function Deploy
{
param(
[Parameter()]
[ValidateNotNullOrEmpty()]
[string]$BuildName=$(throw "BuildName is mandatory, please provide a value.")
)
Write-Host "Build name is: $BuildName"
}