Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare an advanced function with CmdletBinding.
PowerShell
function Get-Greeting {
[[1]()]
param ()
"Hello, World!"
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parameter instead of CmdletBinding.
Forgetting the parentheses after CmdletBinding.
Placing CmdletBinding inside the param block.
✗ Incorrect
The CmdletBinding attribute turns a function into an advanced function, enabling cmdlet features.
2fill in blank
mediumComplete the code to define a mandatory string parameter in an advanced function.
PowerShell
function Get-Greeting {
[CmdletBinding()]
param (
[Parameter(Mandatory=[1])]
[string]$Name
)
"Hello, $Name!"
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using True or False without the $ sign.
Setting Mandatory to $false when the parameter should be required.
✗ Incorrect
The Mandatory parameter attribute requires the user to provide a value; it uses $true to enable it.
3fill in blank
hardFix the error in the advanced function to correctly support pipeline input by property name.
PowerShell
function Get-Greeting {
[CmdletBinding()]
param (
[Parameter(ValueFromPipelineByPropertyName=[1])]
[string]$Name
)
process {
"Hello, $Name!"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using True or False without the $ sign.
Setting ValueFromPipelineByPropertyName to $false disables pipeline input.
✗ Incorrect
To accept pipeline input by property name, ValueFromPipelineByPropertyName must be set to $true.
4fill in blank
hardFill both blanks to add a switch parameter and output a message when it is used.
PowerShell
function Get-Greeting {
[CmdletBinding()]
param (
[switch]$VerboseMode
)
begin {
if ([1]) {
Write-Verbose "[2] mode is on."
}
}
process {
"Hello!"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without $ in the if condition.
Confusing Verbose and VerboseMode parameter names.
✗ Incorrect
The if statement needs the variable with $ sign ($VerboseMode). The Write-Verbose message is about VerboseMode parameter.
5fill in blank
hardFill all three blanks to define an advanced function with a string parameter, pipeline input, and output type.
PowerShell
function Get-Greeting {
[[1]()]
[OutputType([3])]
param (
[Parameter(ValueFromPipelineByPropertyName=[2])]
[string]$Name
)
process {
"Hello, $Name!"
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Parameter instead of CmdletBinding.
Using False instead of $true for pipeline input.
Not specifying output type as [string].
✗ Incorrect
CmdletBinding makes the function advanced, $true enables pipeline input by property name, and [string] declares the output type.