A mandatory parameter is an input that the person or script initiating an automation must supply. Use it for values the automation cannot safely infer or replace with a fixed default. section 10.5
Choose mandatory status by deciding whether the automation can provide a safe alternative:
- Require essential values such as a Hyper-V host, virtual machine name, storage locations, disk sizes, virtual switch, memory, or ISO location. section 10.5
- Keep values fixed when they should not vary, such as disabling automatic checkpoints. section 10.5
- Use optional parameters when a sensible default exists, and logical parameters when script logic can determine a value with a fallback. section 10.5
In PowerShell, mark required function inputs with the Mandatory parameter attribute. Combine it with validation when the input must be nonblank, fit a length limit, or match an approved set of values. section 7.3.1 section 7.3.1
Validating a required server name
A New-PoshServer function can require Name and use ValidateScript to reject names longer than 50 characters. The same pattern can require Status, OSType, RemoteMethod, and Source while ValidateSet prevents inconsistent spellings and abbreviations. section 7.3.1 section 7.3.1
Function New-PoshServer {
param(
[Parameter(Mandatory=$true)]
[ValidateScript({$_.Length -le 50 })]
[string]$Name
)
$PSBoundParameters
}
New-PoshServer -Name 'Srv01'
New-PoshServer -Name 'ThisIsAReallyLongServerNameThatWillCertainlyExceed50Characters'
Inspect the PowerShell listing for a mandatory string parameter paired with a maximum-length validation rule. section 7.3.1
