Bird
0
0

You want to run a script on multiple Linux servers using SSH remoting in PowerShell 7+. Which approach correctly creates sessions and runs the script block on all servers efficiently?

hard📝 Application Q8 of 15
PowerShell - Remote Management
You want to run a script on multiple Linux servers using SSH remoting in PowerShell 7+. Which approach correctly creates sessions and runs the script block on all servers efficiently?
A$servers = @('server1','server2'); $sessions = $servers | ForEach-Object { New-PSSession -HostName $_ -UserName admin }; Invoke-Command -Session $sessions -ScriptBlock { uptime }
B$servers = @('server1','server2'); foreach ($s in $servers) { Invoke-Command -HostName $s -UserName admin -ScriptBlock { uptime } }
C$sessions = New-PSSession -HostName 'server1','server2' -UserName admin; Invoke-Command -Session $sessions -ScriptBlock { uptime }
DInvoke-Command -ComputerName 'server1','server2' -Credential admin -ScriptBlock { uptime }
Step-by-Step Solution
Solution:
  1. Step 1: Understand session creation for multiple SSH hosts

    Creating sessions individually with New-PSSession for each host and collecting them in an array is correct.
  2. Step 2: Use Invoke-Command with session array

    Invoke-Command accepts multiple sessions and runs the script block on all efficiently.
  3. Step 3: Eliminate incorrect options

    $servers = @('server1','server2'); foreach ($s in $servers) { Invoke-Command -HostName $s -UserName admin -ScriptBlock { uptime } } runs commands sequentially, C uses invalid array for -HostName, D uses -ComputerName which is for WinRM.
  4. Final Answer:

    $servers = @('server1','server2'); $sessions = $servers | ForEach-Object { New-PSSession -HostName $_ -UserName admin }; Invoke-Command -Session $sessions -ScriptBlock { uptime } -> Option A
  5. Quick Check:

    Use session array with Invoke-Command for multiple SSH hosts [OK]
Quick Trick: Create sessions per host, then invoke command on all sessions [OK]
Common Mistakes:
  • Using -ComputerName instead of -HostName for SSH
  • Passing array directly to -HostName parameter
  • Running commands sequentially instead of in parallel

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PowerShell Quizzes