0
0
PowerShellscripting~20 mins

Invoke-Command in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Invoke-Command 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 Invoke-Command script?
Consider this PowerShell script that runs a command on a remote computer named 'Server01'. What will be the output?
PowerShell
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Date -Format 'yyyy-MM-dd' }
AThe current date on Server01 in 'yyyy-MM-dd' format, e.g., '2024-06-15'
BThe current date on the local computer in 'yyyy-MM-dd' format
CAn error saying 'ComputerName Server01 not found'
DNo output, the command runs silently
Attempts:
2 left
💡 Hint
Invoke-Command runs the script block on the remote computer specified by -ComputerName.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses Invoke-Command to run a script block on multiple computers?
You want to run the command 'hostname' on two remote computers: 'PC1' and 'PC2'. Which syntax is correct?
AInvoke-Command -ComputerName PC1, PC2 -ScriptBlock { hostname }
BInvoke-Command -ComputerName 'PC1 PC2' -ScriptBlock { hostname }
CInvoke-Command -ComputerName @('PC1','PC2') -ScriptBlock { hostname }
DInvoke-Command -ComputerName ['PC1','PC2'] -ScriptBlock { hostname }
Attempts:
2 left
💡 Hint
The -ComputerName parameter accepts a comma-separated list of computer names.
🔧 Debug
advanced
2:00remaining
Why does this Invoke-Command script fail with an error?
This script tries to run a command on a remote computer but fails. What is the cause?
PowerShell
Invoke-Command -ComputerName Server01 -ScriptBlock { Get-Process } -Credential $null
AThe script block is invalid because Get-Process cannot run remotely.
BPassing $null to -Credential causes an error because a valid credential is required for remote sessions.
CThe ComputerName parameter is missing quotes around Server01.
DInvoke-Command does not support the -Credential parameter.
Attempts:
2 left
💡 Hint
Check the use of the -Credential parameter and what values it accepts.
🚀 Application
advanced
2:00remaining
How to capture output from multiple remote computers using Invoke-Command?
You want to run 'Get-Service' on 'ServerA' and 'ServerB' and save the combined output locally. Which script achieves this?
A$results = Invoke-Command -ComputerName ServerA, ServerB -ScriptBlock { Get-Service }; $results
BInvoke-Command -ComputerName ServerA, ServerB -ScriptBlock { Get-Service } | Out-File services.txt
C$results = Invoke-Command -ComputerName ServerA -ScriptBlock { Get-Service }; $results += Invoke-Command -ComputerName ServerB -ScriptBlock { Get-Service }; $results
DInvoke-Command -ComputerName ServerA, ServerB -ScriptBlock { Get-Service } > services.txt
Attempts:
2 left
💡 Hint
Invoke-Command returns output objects that can be stored in a variable.
🧠 Conceptual
expert
2:00remaining
What happens if you use Invoke-Command with -AsJob parameter?
You run this command: Invoke-Command -ComputerName Server01 -ScriptBlock { Start-Sleep -Seconds 10; 'Done' } -AsJob. What is the behavior?
AThe command runs asynchronously but outputs 'Done' immediately.
BThe command runs synchronously and outputs 'Done' after 10 seconds.
CThe command fails because -AsJob cannot be used with Invoke-Command.
DThe command runs asynchronously as a background job; you must use Receive-Job to get the output later.
Attempts:
2 left
💡 Hint
The -AsJob parameter runs the command in the background.