liveBook · Manning

Mandatory parameter

As explained in Practical Automation with PowerShell: Effective scripting from the console to the cloud

A mandatory parameter is an input that must be supplied by the person or script initiating an automation.

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

How Practical Automation with PowerShell: Effective scripting from the console to the cloud covers it

How deep it goes
Explains required-input classification and PowerShell attributes, then adds string and enumerated-value validation without covering enumeration implementations.
Examples
Uses Hyper-V virtual machine creation and a New-PoshServer function with required names, constrained lengths, and approved value sets.
What it assumes
Assumes familiarity with PowerShell functions and parameters while establishing required, optional, fixed, and logical input categories.
What you can do after
Apply it via PowerShell functions to require inputs and reject blank, oversized, misspelled, or unsupported values.
How it connects
Connects mandatory parameters with optional parameters, fixed values, logical parameters, ValidateScript, and ValidateSet.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practical Automation with PowerShell: Effective scripting from the console to the cloud. The section links point to the parts of the book it draws on.