Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add a ScriptMethod named 'Greet' to the object $obj.
PowerShell
$obj.PSObject.Members.Add((New-Object System.Management.Automation.PSMethod('Greet', [1])))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Write-Host inside the script block instead of returning a value.
Not using a script block as the second argument.
Omitting param() in the script block.
✗ Incorrect
The ScriptMethod requires a script block as the second argument. Using { param() 'Hello!' } defines a method that returns 'Hello!'.
2fill in blank
mediumComplete the code to call the 'Greet' method on the object $obj.
PowerShell
$message = $obj.[1]() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the one added.
Forgetting the parentheses to call the method.
✗ Incorrect
The method added was named 'Greet', so calling $obj.Greet() executes it.
3fill in blank
hardFix the error in adding a ScriptMethod that returns the object's name property.
PowerShell
$obj.PSObject.Members.Add((New-Object System.Management.Automation.PSMethod('GetName', [1])))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using $obj inside the script block which is not defined there.
Omitting param() causing errors.
Not returning the value explicitly.
✗ Incorrect
Inside a ScriptMethod, $this refers to the object. Using param() and return ensures the method returns the Name property.
4fill in blank
hardFill the blank to add a ScriptMethod 'AddNumbers' that takes two parameters and returns their sum.
PowerShell
$obj.PSObject.Members.Add((New-Object System.Management.Automation.PSMethod('AddNumbers', [1])))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not defining parameters in the script block.
Omitting the script block syntax {}.
Using incorrect syntax for the script block.
✗ Incorrect
The ScriptMethod needs a script block with parameters $a and $b. { param($a, $b) $a + $b } defines the method that implicitly returns their sum.
5fill in blank
hardFill all three blanks to create an object $obj with a ScriptMethod 'Describe' that returns a string with the object's Name and Age.
PowerShell
$obj = New-Object PSObject -Property @{ Name = 'Alice'; Age = 30 }
$scriptBlock = [1]
$obj.PSObject.Members.Add((New-Object System.Management.Automation.PSMethod('Describe', $scriptBlock)))
$result = $obj.[2]()
Write-Output [3] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not using $this to access properties inside the script block.
Calling the method incorrectly without parentheses.
Trying to output the method name instead of the result variable.
✗ Incorrect
The script block returns a formatted string using $this to access properties. The method 'Describe' is called, and its result stored in $result, which is output.