SSH-based remoting (PowerShell 7+) - Time & Space Complexity
When using SSH-based remoting in PowerShell, it's important to understand how the time to run commands grows as you work with more remote sessions or larger data. We want to see how the execution time changes when we run commands remotely over SSH.
The question is: How does the time to complete remote commands grow as we increase the number of commands or sessions?
Analyze the time complexity of the following code snippet.
# Create multiple remote sessions over SSH
$sessions = 1..n | ForEach-Object { New-PSSession -HostName "server$_" -UserName "user" -SSHTransport }
# Run a command on each session
$sessions | ForEach-Object { Invoke-Command -Session $_ -ScriptBlock { Get-Process } }
# Close all sessions
$sessions | ForEach-Object { Remove-PSSession $_ }
This code creates n SSH remote sessions, runs a command on each, then closes them.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping over n sessions to create, invoke commands, and remove sessions.
- How many times: Each operation runs exactly n times, once per session.
As the number of sessions n increases, the total time grows roughly in direct proportion to n because each session requires setup, command execution, and teardown.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 session setups, 10 command runs, 10 session removals |
| 100 | About 100 session setups, 100 command runs, 100 session removals |
| 1000 | About 1000 session setups, 1000 command runs, 1000 session removals |
Pattern observation: The total work grows linearly as you add more sessions.
Time Complexity: O(n)
This means the total time to complete all remote commands grows in a straight line as you add more sessions.
[X] Wrong: "Running commands on multiple sessions happens all at once, so time stays the same no matter how many sessions."
[OK] Correct: Each session setup and command runs separately, so total time adds up with more sessions, not stays constant.
Understanding how remote commands scale helps you design scripts that run efficiently and predict how long tasks will take when managing many servers.
"What if we ran commands on all sessions in parallel instead of one after another? How would the time complexity change?"
Practice
Solution
Step 1: Understand SSH-based remoting concept
SSH-based remoting allows secure communication to run commands on other computers remotely.Step 2: Identify the main use case in PowerShell 7+
PowerShell 7+ uses SSH remoting to securely run commands or scripts on remote machines.Final Answer:
To securely run commands on remote computers using PowerShell -> Option BQuick Check:
SSH remoting = Secure remote command execution [OK]
- Confusing remoting with local script execution
- Thinking SSH remoting creates GUIs
- Assuming it compiles scripts
Server01?Solution
Step 1: Recall Enter-PSSession syntax for SSH remoting
PowerShell 7+ usesEnter-PSSession -HostName <name>to start SSH sessions.Step 2: Compare options
Enter-PSSession -HostName Server01 uses-HostName Server01, which is correct for SSH remoting. Enter-PSSession -ComputerName Server01 -UseSSH uses-ComputerNamewith-UseSSHwhich is invalid syntax. Invoke-Command -Session Server01 -SSH mixes parameters incorrectly. New-PSSession -ComputerName Server01 creates a session but does not specify SSH or hostname properly.Final Answer:
Enter-PSSession -HostName Server01 -> Option AQuick Check:
SSH session uses -HostName parameter [OK]
- Using -ComputerName instead of -HostName for SSH
- Mixing Invoke-Command with session parameters
- Omitting SSH-specific parameters
Server01 is reachable and has PowerShell 7+ installed?Invoke-Command -HostName Server01 -ScriptBlock { Get-Date }Solution
Step 1: Understand Invoke-Command with -HostName
This runs the script block on the remote computer specified by -HostName.Step 2: Analyze the script block
The script block callsGet-Date, which returns the current date/time on the remote machine.Final Answer:
The current date and time from Server01 -> Option AQuick Check:
Invoke-Command with -HostName runs remotely [OK]
- Expecting local date/time output
- Confusing parameter names
- Assuming no output is shown
Enter-PSSession -HostName Server01
The error says: "The term 'Enter-PSSession' is not recognized." What is the most likely cause?
Solution
Step 1: Understand the error message
The error says 'Enter-PSSession' is not recognized, meaning the command is missing locally.Step 2: Identify cause related to PowerShell version
Enter-PSSession with -HostName for SSH remoting is available only in PowerShell 7+. Older versions do not support it.Final Answer:
PowerShell version is older than 7 on the local machine -> Option DQuick Check:
Command not recognized = old PowerShell version [OK]
- Assuming remote offline causes local command error
- Blaming SSH service without checking local version
- Thinking username is mandatory for command recognition
Server01 and Server02 using SSH remoting in PowerShell 7+. Which command correctly runs Get-Process on both and collects results?Solution
Step 1: Understand running commands on multiple SSH hosts
Invoke-Command -HostName accepts an array of multiple host names directly in PowerShell 7+.Step 2: Analyze options
Invoke-Command -HostName Server01,Server02 -ScriptBlock { Get-Process } runs Get-Process on both remote servers and collects the results.Step 3: Why other options are incorrect
A uses New-PSSession -ComputerName, which targets WinRM not SSH. B uses -ComputerName with invalid -UseSSH. C enters sessions sequentially but Get-Process runs locally after.Final Answer:
Invoke-Command -HostName Server01,Server02 -ScriptBlock { Get-Process } -> Option CQuick Check:
Multiple SSH hosts: -HostName Server01,Server02 [OK]
- Using -ComputerName (WinRM) instead of -HostName for SSH
- Using invalid -UseSSH parameter
- Running commands locally after sequential Enter-PSSession
Ready to prove it?
Pass the test and this pattern moves unknown
