0
0
PowerShellscripting~20 mins

Adding methods with ScriptMethod in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
ScriptMethod Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this PowerShell script adding a ScriptMethod?
Consider this PowerShell code that adds a ScriptMethod to a custom object. What will be the output when calling the method?
PowerShell
$obj = New-Object PSObject -Property @{Name='Alice'}
$scriptBlock = { "Hello, $($this.Name)!" }
$obj | Add-Member -MemberType ScriptMethod -Name Greet -Value $scriptBlock
$obj.Greet()
AHello, Alice!
BHello, $this.Name!
CHello, Name!
DError: Method not found
Attempts:
2 left
💡 Hint
Remember that $this inside a ScriptMethod refers to the object itself.
📝 Syntax
intermediate
2:00remaining
Which option correctly adds a ScriptMethod to a PowerShell object?
You want to add a ScriptMethod named 'SayBye' to a PSObject that returns 'Goodbye, !'. Which code snippet is correct?
A$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, this.Name!" }
B$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, $Name!" }
C$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, $this->Name!" }
D$obj | Add-Member -MemberType ScriptMethod -Name SayBye -Value { "Goodbye, $($this.Name)!" }
Attempts:
2 left
💡 Hint
Inside ScriptMethod, use $this to refer to the object itself.
🔧 Debug
advanced
2:00remaining
Why does this ScriptMethod fail to access the object's property?
Given this code, why does calling $obj.Hello() produce an error? $obj = New-Object PSObject -Property @{Name='Bob'} $sb = { "Hi, $Name!" } $obj | Add-Member -MemberType ScriptMethod -Name Hello -Value $sb $obj.Hello()
ABecause $Name is not defined inside the script block; should use $this.Name
BBecause Add-Member requires -PassThru to work with ScriptMethod
CBecause PSObject cannot have ScriptMethods added
DBecause the script block must be a string, not a script block
Attempts:
2 left
💡 Hint
Think about how to access object properties inside ScriptMethod script blocks.
🚀 Application
advanced
2:00remaining
How to add a ScriptMethod that modifies an object property?
You want to add a ScriptMethod 'SetAge' to a PSObject that sets its 'Age' property to a given value. Which code correctly does this?
A$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { $Age = $args[0] }
B$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { param($a) $this.Age = $a }
C$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { param($a) $Age = $a }
D$obj | Add-Member -MemberType ScriptMethod -Name SetAge -Value { param($a) this.Age = $a }
Attempts:
2 left
💡 Hint
Use $this to refer to the object and param() to accept arguments.
🧠 Conceptual
expert
2:00remaining
What happens if you add multiple ScriptMethods with the same name to a PSObject?
If you add two ScriptMethods named 'Action' to the same PSObject, what will be the result when calling $obj.Action()?
ABoth methods run sequentially when calling $obj.Action()
BThe last added ScriptMethod overwrites the previous one; calling $obj.Action() runs the last method
CPowerShell throws an error when adding a duplicate ScriptMethod
DCalling $obj.Action() runs the first added method, ignoring the second
Attempts:
2 left
💡 Hint
Add-Member throws an error when adding a member with a name that already exists unless -Force is specified.