Specifying parameter attributes

In this section, we will discuss the parameter attributes and how to set them. The attributes falling under this category define the different attributes of the parameter itself. Let's take a closer look at the most useful and common options available to define parameter attributes and their uses:

  • Mandatory argument: This argument indicates that this particular parameter is compulsory, otherwise it is optional. For example, if I am writing a function to connect to a vCenter server and doing some work and I want the vCenter name to be provided at runtime, then the following code makes sure that the cmdlet call will fail without the $VCName parameter:
    Param (
      [parameter(Mandatory=$true)]
      [String]$VCName
    )
  • Position argument: ...

Get Mastering PowerCLI now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.