Remoting
PowerShell Remote Control
- WinRM
- ssh (PowerShell 6 or later)
WinRM
Preparation
Prepare two PCs. (Local PC, Remote PC)
Start PowerShell with administrative privileges.
The first line is to configure WinRM to be enabled.
The second line is to configure WinRM trusted connections.
The “-Value *” specifies that all hosts are trusted.
If you want to limit the number of connections, you can specify it here (for non-domain environments).
PS> Enable-PSRemoting -Force
PS> Set-Item wsman:\localhost\Client\TrustedHosts -Value * -Force # In a non-domain environment, this is also an additional step.
Start an interactive session (Enter-PSSession)
The Enter-PSSession cmdlet starts an interactive session, which can be terminated with exit or other commands.
Not only this command, but also “-Credential” can be used to specify credentials to start.
PS C:\Users\user\Desktop> Enter-PSSession -ComputerName COMPUTER0001
[COMPUTER0001]: PS C:\Users\user\Documents> exit
PS C:\Users\user\Desktop> Enter-PSSession -ComputerName COMPUTER0001 -Credential UserName
[COMPUTER0001]: PS C:\Users\UserName\Documents>
Remotely execute a command (Invoke-Command)
You can specify a script block.
PS C:\Users\user\Desktop> Invoke-Command -ComputerName COMPUTER0001 {Get-Process}
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName PSComputerName
------- ------ ----- ----- ------ -- -- ----------- --------------
51 4 1872 3840 0.02 2072 0 Process COMPUTER0001
~Abbreviated as follows~
You can specify a script file.
# The content of script.sp1 is "hostname".
PS C:\Users\user\Desktop> Invoke-Command -ComputerName COMPUTER0001 -FilePath .\hostname.ps1
COMPUTER0001
Using a PS session
If you specify -ComputerName
directly when using the cmdlet,
a PS session will be created and extinguished each time the cmdlet is executed.
If you want the PS session to continue, create a session.
PS C:\Users\user\Desktop> Invoke-Command -ComputerName COMPUTER0001 {$test = "test"}
PS C:\Users\user\Desktop> Invoke-Command -ComputerName COMPUTER0001 {$test}
PS C:\Users\user\Desktop> # The contents of $test are not displayed, because PS sessions are being created and deleted.
PS C:\Users\user\Desktop> $session = New-PSSession COMPUTER0001
PS C:\Users\user\Desktop> Invoke-Command $session {$test = "test"}
PS C:\Users\user\Desktop> Invoke-Command $session {$test}
test # The contents of $test are now displayed. Because the PS session is saved in $session.
PS C:\Users\user\Desktop> Enter-PSSession $session # You can use $session in other cmdlets.
[COMPUTER0001]: PS C:\Users\user\Documents> $test
test
Sessions and reconnection
Available in PowerShell V3 and later.
Remote sessions can be disconnected and reconnected.
PS C:\Users\user\Desktop> New-PSSession COMPUTER0001
Id Name ComputerName ComputerType State ConfigurationName Availability
-- ---- ------------ ------------ ----- ----------------- ------------
11 Session11 COMPUTER0001 RemoteMachine Opened Microsoft.PowerShell Available
PS C:\Users\user\Desktop> $session = Get-PSSession -Id 11
PS C:\Users\user\Desktop> Invoke-Command $session {$a = 1}
PS C:\Users\user\Desktop> Disconnect-PSSession $session
Id Name ComputerName ComputerType State ConfigurationName Availability
-- ---- ------------ ------------ ----- ----------------- ------------
11 Session11 COMPUTER0001 RemoteMachine Disconnected Microsoft.PowerShell None
# ↑From Opened to Disconnected.
Now close the console and start up another console.
PS C:\Users\user\Desktop> Get-PSSession
PS C:\Users\user\Desktop> Get-PSSession -ComputerName COMPUTER0001
Id Name ComputerName ComputerType State ConfigurationName Availability
-- ---- ------------ ------------ ----- ----------------- ------------
1 Session11 COMPUTER0001 RemoteMachine Disconnected Microsoft.PowerShell None
PS C:\Users\user\Desktop> $savedSession = Get-PSSession -ComputerName COMPUTER0001
PS C:\Users\user\Desktop> Connect-PSSession $savedSession
Id Name ComputerName ComputerType State ConfigurationName Availability
-- ---- ------------ ------------ ----- ----------------- ------------
2 Session11 COMPUTER0001 RemoteMachine Opened Microsoft.PowerShell Available
PS C:\Users\user\Desktop> Invoke-Command $savedSession {$a}
1 # The value you have just assigned is also stored.