Powershell

Table of Contents

Sessions



Traditional approaches to administering a remote computer involve locating the device on the network then using a remote desktop application to log into the device. In this approach a session is created with the remote computer using an application such as Windows Remote Desktop to remote into the device GUI interface. PowerShell nativly provides a
New-PSSession
command to create a PowerShell CLI Interface with one or many devices in a persistent link.

PowerShell also allows the functionality to use the
Invoke-Command
command to run commands or scripts against a target device and can be combined with Jobs and Tasks to run continuous commands against the host target for a result. This can also be achieved natively in windows using Command prompt, and with other operating systems such as Linux and MacOS through its default UNIX Shell.

In the first instance, a Session is created to allow persistent synchronous transaction of data back and forth between the source device and target devices e.g.

New-PSSession -ComputerName Server01,Server02,Server03


furthermore each computer specified can be broken into its own session in one command e.g.

$session1, $session2,$session3 = New-PSSession -ComputerName Server01,Server02,Server03


These sessions can be stored into variables e.g.

$session1 = New-PSSession –ComputerName "Server01"


to allow further commands to be run against them. Sessions exist until closed either by removing the session from the source Powershell device, or through power/policy elimination on the target device.

You may run commands on a remote computer without creating a PowerShell Session by using the -ComputerName parameters of
Enter-PSSession
or
Invoke-Command
commands. When you use the -ComputerName parameter, PowerShell creates a temporary connection that is used for the command and is then closed. The results of the commands above will not be displayed on the source device, instead PowerShell derives result codes stating the success of target command on remote device.

By creating a PowerShell session using the
New-PSSession
command one can continue working and have results displayed back to them as if one was on the target machine itself. Once the session is created existing sessions can be viewed with the
Get-PSSession
command.


SSH (SSL)



From PowerShell version 6.0 onwards you may use Secure Shell (SSH) using the –UseSSL parameter within
New-PSSession
command to establish a connection and create a session on a remote computer as long as SSH is available on the local computer and the remote computer is configured with a PowerShell SSH endpoint. This allows the PowerShell remote session to work across multiple platforms (Windows, Linux, macOS).

E.g. the below command uses SSL to establish a SSL Session with the –UseSSL parameter then by using a specified port number in the –Port parameter. The Port number will need to be configured on the target device by setting up a WinRM listener on destination device. Once done a new session will be made to the Server.

New-PSSession -ComputerName Server01 -Port 8081 -UseSSL


You are also able to create a PowerShell session using SSH by placing the username and target device in the –HostName parameter with the port number and specifying the SSH key destination in the –KeyFilePath parameter. The –KeyFilePath parameter is not required and without the command will prompt for a login for the user.

New-PSSession -HostName admin@DC011:88 -KeyFilePath c:\keys\admin_rsa


Create a session by using a URI

$session = New-PSSession -URI http://DC01:8181/NewSession -Credential poshpython.local\administrator


This command creates a PSSession on the DC01 server and stores it in the $session variable. It uses the -URI parameter to specify the transport protocol, the remote computer, the port, and an alternate session configuration. It also uses the -Credential parameter to specify a user account "administrator" that has permission to create a session on the remote computer.

Run a background job in a set of sessions

$session = New-PSSession -ComputerName (Get-Content Servers.txt) -Credential poshpython.local\administrator -ThrottleLimit 16 Invoke-Command -Session $session -ScriptBlock {Get-Process PowerShell} -AsJob


These commands create a set of PSSession objects and then run a background job in each of the PSSession objects. The first command creates a new PSSession on each of the computers listed in the Servers.txt file. It uses the New-PSSession cmdlet to create the PSSession. The value of the -ComputerName parameter is a command that uses the
Get-Content
command to get the list of computer names the Servers.txt file

Official Microsoft Knowledge Base documentation on New-PSSession found by clicking this link here


Visit next page to learn about - Functions