Types of remoting

There are three types of remoting: Interactive (or 1:1), Fan-Out (or 1:n), and Implicit. Interactive remoting is usually used when troubleshooting issues interactively. One session can be entered, and you can interactively run commands in it; when you are done, the session is torn down:

# Interactive, 1:1
# Session is created, used interactively and exited
Enter-PSSession -ComputerName Machine1
Get-Host
Exit-PSSession

# 1:n
$sessions = New-PSSession -ComputerName Machine1,Machine2,MachineN

# Persistent sessions, Invoke-Command uses 32 parallel connections
# Can be controlled with ThrottleLimit
Invoke-Command -Session $sessions -ScriptBlock {
$variable = "value"
}

# Persistent sessions can be used multiple times until the idle timeout
# destroys the session on the target machine
Invoke-Command -Session $sessions -ScriptBlock {
Write-Host "`$variable is still $variable"
}

# Persistent sessions can even be reconnected to if PowerShell has been closed
# If the same authentication options are used, a reconnect is possible
Get-PSSession -ComputerName Machine1

# DisconnectedScope
# Invoke-Command can run in a disconnected session that can be connected
# from any other machine on the network unless it is already connected
Invoke-Command -ComputerName Machine1 -InDisconnectedSession -ScriptBlock {
Start-Sleep -Seconds 500 # Some long running operation
}

# Either
Connect-PSSession -ComputerName Machine1 # Simply connects session so it can be used

# Or
Receive-PSSession -ComputerName Machine1 # Attempts to connect session and also retrieve data

Fanning out is the most common use case. One management machine connects to multiple remote machines to retrieve data, change settings, and run scripts:

# Implicit
# You can import sessions and modules from sessions. This is called implicit remoting
$s = New-PSSession -ComputerName DomainController
Import-Module -PSSession $s -Name ActiveDirectory -Prefix contoso
Get-contosoAdUser -Filter * # Cmdlet runs remotely and is used locally

# Data types, Serialization
Invoke-Command -ComputerName Machine1 -ScriptBlock {
"hello"
} | Get-Member # Added properties PSComputerName, RunspaceId, ShowComputerName

# Complex data types are deserialized and lose their object methods
# However, all Properties still exist and usually even keep the data type
# unless it also has been deserialized
(Invoke-Command -ComputerName Machine1 -ScriptBlock {
Get-Process
}).GetType().FullName # Deserialized.System.Diagnostics.Process

Implicit remoting is a very cool technique, used to import sessions or modules from sessions. That means that you can use any machine to manage machines with Windows PowerShell cmdlets. You can, for example, manage your Active Directory domain from a CentOS client, by importing the Active Directory cmdlets from a session. Those cmdlets can then be used like locally installed module cmdlets, and remoting is done implicitly; hence, the name.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.144.197