Bird
0
0

You want to run a command on multiple remote computers and keep the sessions open for later use. Which approach correctly creates sessions for 'Server01' and 'Server02' and stores them for reuse?

hard📝 Application Q15 of 15
PowerShell - Remote Management
You want to run a command on multiple remote computers and keep the sessions open for later use. Which approach correctly creates sessions for 'Server01' and 'Server02' and stores them for reuse?
A$sessions = New-PSSession -ComputerName Server01 $sessions += New-PSSession -ComputerName Server02 Invoke-Command -Session $sessions -ScriptBlock { hostname }
B$sessions = New-PSSession -ComputerName Server01, Server02 Invoke-Command -ComputerName Server01, Server02 -ScriptBlock { hostname }
CInvoke-Command -ComputerName Server01, Server02 -ScriptBlock { hostname } $sessions = $null
D$sessions = New-PSSession -ComputerName Server01 Invoke-Command -Session $sessions -ScriptBlock { hostname } Remove-PSSession -Session $sessions
Step-by-Step Solution
Solution:
  1. Step 1: Recall how to create persistent sessions for multiple computers

    New-PSSession can take multiple ComputerName values or create separately and collect into an array with +=. Invoke-Command must use -Session $sessions (array) to run on persistent sessions.
  2. Step 2: Evaluate each option

    A creates sessions but uses -ComputerName on Invoke-Command, creating temporary connections instead of using persistent ones. B builds the array explicitly and uses -Session. C uses temporary only. D removes sessions.
  3. Final Answer:

    $sessions = New-PSSession -ComputerName Server01 $sessions += New-PSSession -ComputerName Server02 Invoke-Command -Session $sessions -ScriptBlock { hostname } -> Option A
  4. Quick Check:

    Build session array with +=, use -Session [OK]
Quick Trick: Create and store sessions in a variable array for reuse [OK]
Common Mistakes:
  • Removing sessions immediately before reuse
  • Not storing sessions in a variable
  • Using Invoke-Command without sessions for reuse

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes