Remote PowerShell Session to Server Core

PowerShell

Hello Folks, this is just a simple post on how to open and enter a Remote PowerShell Session from one server to another. A lot of tasks on different servers can be accomplished in a convenient way from one server.

For instance if you want to Change the Keyboard Layout on the login screen of a new Server Core installation.

We will use the cmdlets below to open, enter, exit, and remove a Remote PowerShell session.

Remote PowerShell Prerequisites:

To receive PowerShell remote commands PS-Remoting must be enabled on the computer. PS-Remoting is enabled by default. Further the WinRM Service needs to be started.

If you want to go for sure firstly check the PS-Remoting state of the remote computer with:

Test-WSMan -ComputerName NameOfRemoteComputer

Afterward, verify the state of the WinRM service on the remote computer that it is running:

Get-Service WinRM -ComputerName NameOfRemoteComputer | Select MachineName,Name,Status

Remote PowerShell Session Cmdlets:

Get-PSSession – [Shows current sessions]
New-PSSession – [Create a new persistent connection to the target host]
Enter-PSSession – [Enter a session as interactive session]
Exit-PSSession – [Exit session. The session will still be alive in the background]
Disconnect-PSSession – [Disconnect from the session]
Connect-PSSession – [connect to a disconnected session]
Remove-PSSession – [Remove an earlier created persistent session]

Ok, here we go …

1 -Create a Remote PowerShell Session with New-PSSession:

New-PSSession -Name NameOfSession -ComputerName NameOfRemoteComputer -Credential Domain\Username.

In case you don’t want to give the remote PowerShell session a name it will be named WinRM*

2 -Check the newly created session with Get-PSSession:

Get-PSSession

The output will show the state of the session:
Get Remote PowerShell SessionI gave my session the name “ServerCore”. It connects to an Exchange 2019 server running on Server 2019 Core with hostname “exchange2019”

If you have more sessions you can go for the computer name:
Get-PSSession -ComputerName NameOfRemoteComputer

… or explicit for the session name:
Get-PSSession -Name NameOfSession

… and also for the Id:
Get-PSSession -Name ID

3 -Enter a session directly as an interactive session with Enter-PSSession:

Enter-PSSession -ComputerName NameOfRemoteComputer
Enter-PSSession -name NameOfSession.
 
As a result, PowerShell switches to the entered interactive session:
 
Enter PowerShell Session
You can perform tasks on the target server via PowerShell from the localhost within this session now.
Enter-PSSession does not require a session that has been created with New-PSSession before.

Find more on how to run Remote Commands from a Remote PowerShell Session here.

4 -Exit an interactive session with Exit-PSSession:

Within the remote PowerShell type:
Exit-PSSession

Instead of Exit-PSSession, you can just type Exit as well. It will have the same effect.

Thereafter, the session remains open but you have left the interactive session into the localhost’s PowerShell:

Open Remote PowerShell Sessions

5 -Disconnect a session with Disconnect-PSSession:

You can disconnect a remote PowerShell session which has been created with New-PSSession.
But you can’t disconnect an interactive session that has been initiated with Enter-PSSession.

To disconnect a session run the command below from your localhost’s PowerShell:

Disconnect-PSSession -name
NameOfSession 

…or disconnect it based on the ID:
Disconnect-PSSession -Id ID 

Further, you can disconnect all remote session for a certain remote host based on ComputerName:
Disconnect-PSSession -Id ComputerName 

Following you get an output like this:
Disconnect Remote PowerShell Session

You can later reconnect by using the Connect-PSSession cmdlet.

6 -Reconnect a disconnected PowerShell session:

As shown below you can reconnect an existing session that has been created with New-PSSession:

Connect-PSSession -name NameOfSession… or you connect it with its ID:
Connect-PSSession -Id ID… as well as with ComputerName:
Connect-PSSession -Id Computername

Remote PowerShell Session to Exchange Server

To connect with a Remote PowerShell Session to Exchange use:

$UserCred = Get-Credential

$ExSession = New-PSSession -ConfigurationName Microsoft.Exchange –Name ExchangeSession -ConnectionUri “http://RemoteExchangeFQDN/powershell” -Credential $UserCred -Authentication Kerberos

Import-PSSession $ExSession

You can verify the session with name ExchangeSession you just openend with Get-PSSession:

Get Remote PowerShell Session Exchange
The Exchange CMDLets will be available now.

The same can be done from an Exchange Management Shell to connect through a Remote PowerShell session to another Exchange Server:

Open an Exchange Management Shell

If you are on a Server Core type LaunchEMS into the command window.

Use the CMDlets from above

That’s all.  smiley :)
 
 
To display the output of a PowerShell command completely without ellipses take a look here:

Expand PowerShell Output
 

Leave a Reply

Your email address will not be published. Required fields are marked *