PowerShell Studio: Working with Remote Systems (Part 2)

This is the second post in a blog series about working with remote systems using PowerShell Studio.

The following topics are covered:
Part 1 – Caching Remote Systems PowerShell Modules
Part 2 – PowerShell remoting

In the previous blog post I covered how to use the Cache Export tool to cache module information from a remote system using the Import Remote Cache button, which makes the modules available in the PowerShell Studio Object Browser.

  

 

Part 2 – PowerShell remoting

In this blog post I will show you how to use Windows PowerShell Remoting for remote scripting using PowerShell Studio.

The following topics will be covered:

  • Prerequisite for PowerShell Remoting
  • Adding Functionality using PowerShell Profile
  • PSSession Configuration setup
  • PSSession Configuration name
  • PowerShell Studio “Remote Console”
  • Creating a PSSession Console Host

 

Prerequisite for PowerShell Remoting

Before we start we need to configure our system to support Windows PowerShell Remote with PowerShell Studio.

First, verify that PowerShell is enabled for Remoting by executing the following one-liner:

Enable-PSRemoting -Force

Note: This command enables the computer to receive remote commands via the WS-Management service.

Next, verify that the WS_Management service is running (see image below).  By default, this service will remain in Stopped status with the Startup Type set to Manual.

For more information on how to setup PowerShell Remoting, see these PowerShell Help topics:

  • about_Remote
  • about_Remote_Troubleshooting
## - Display PowerShell Help Topics:
Get-Help about_Remote -Detailed
Get-Help about_Remote_Troubleshooting

Note: Remember that PowerShell help information is available in your system. Execute the “Update-Help -force” command to download PowerShell help topics and cmdlets documentation.

 

Adding Functionality using PowerShell Profile

Although it is not mandatory to create a PowerShell Profile, it is beneficial when adding your own set of custom functions and also when adding PSSnapin’s because they are made available when the PowerShell console is active.

Keep in mind, PowerShell Snap-ins are not modules and are not loaded automatically.  If you are considering using a PowerShell Profile, it will be useful to add PowerShell Snap-ins during the PowerShell Remoting sessions in either Windows PowerShell and PowerShell Studio console.

For more information about how to create a Windows PowerShell Profile and .NET Snap-ins, see these PowerShell Help topics:

  • about_Profiles
  • about_PsSnapins
## - Display PowerShell Help Topics:
Get-Help about_Profiles -Detailed
Get-Help about_PsSnapins

 

PSSession Configuration setup

These steps will need to take place on the remote system that will be accessed from Windows PowerShell, or from PowerShell Studio Consoles.

  1. Create a simple PowerShell Profile
  2. PSSession Startup Script

 

Create a simple PowerShell Profile

The following steps will create a new Windows PowerShell Profile used for All Users and All Hosts. Just make sure to open the Windows PowerShell Console using Run as Administrator:

'function myProfile{ "My System $Env:COMPUTERNAME profile has been loaded!`r`n"+"$($PSVersionTable.PSVersion)"}; MyProfile;' | Out-File $PsHome\Profile.ps1;

 

PSSession Startup Script

Now, in order for the Windows PowerShell Remote session to automatically load the profile, we need to use the Register-PSSessionConfiguration cmdlet with the ‘-StartupScript‘  parameter to include the location of the profile that we just created:

Register-PSSessionConfiguration -Name MyPSSessionProfile -StartupScript $PsHome\Profile.ps1

For more information about how to create a PSSession configuration, see these PowerShell Help topics:

  • Register-PSSessionConfiguration
## - Display PowerShell Help Topics: 
Get-Help Register-PSSessionConfiguration

 

PSSession Configuration name

Finally, using either the Windows PowerShell Console or the PowerShell Studio PowerShell Console, we proceed to create a PowerShell Remote session from our local machine to the remote system. Using the Enter-PSSession cmdlet with the parameter ‘-ConfigurationName’ followed by the name we registered in the PSSession configuraiton command.  This will load the PowerShell Profile during the remote session.

Use the the following one-liner with the correct system name and credentials in either of the Windows PowerShell or the PowerShell Studio console:

Enter-PSSession -ComputerName W2k16SHP01 -Credential trinity\maxt -ConfigurationName MyPSSessionProfile

   

This will verify that we have a successful remote connection to another system via PowerShell Remoting.

 

PowerShell Studio “Remote Console”

If you don’t need to use the PowerShell Profile, PowerShell Studio gives you the ability to do remote scripting thru the Remote Console button found under the Platform ribbon menu.

  

The Remote Console is good for basic system-to-system remoting without the need to create a PSSession configuration name.

 

Creating a PSSession Console Host

If you’re constantly managing multiple systems, it would tedious to perform these steps every time:

  1. Open a script.
  2. Type, or highlight the Enter-PSSession one-liner.
  3. Then, run in console to connect to the remote system.
  4. Repeat the steps for another system.

PowerShell Studio allows for creating additional Console hosts, and we can create remote connections to individual systems. This can be done using the Options button in the Windows ribbon menu. You can add custom consoles in the Console section.

The following sample will be used to create a new console host to remote connect to another system. This will require the name for the Console host, and the command line to execute:

Name: PSSessionToRemoteSystem1

Command line: C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -noexit -Command “Enter-PSSession -ComputerName SystemName -Credential Domain\UserName -ConfigurationName MyPSSessionName”;

Note: Please notice that the one-liner has the system name and credentials hard coded. Also, the parameter ‘-ConfigurationName’ is optional.

When you have added the new console host command line, exit the Option panel and restart PowerShell Studio.

The next time you click on the Console panel, the newly added PSSession Console is available in the console drop-down list:

Select the newly added console “MyPSSessionXXX”, and this will remote connect to the other system.

 

Conclusion

You need to properly configure and test your Windows PowerShell environment to support PSRemoting before you start working with PowerShell Studio against remote systems. Taking advantage of the PowerShell Help documentation and PowerShell Studio features will assist in extending both your local and remote scripting to work interactively with PowerShell on any system.

Note: The overall concept explained in this blog post can be applied to our PrimalScript editor.

 

Related Articles

1. Developing PowerShell scripts for other machines (Part 2).
2. But it works on my machine…

 

Feedback

As always, if you have any ideas, comments, or feedback, please visit our feedback forum and reference this post.

 

Max Trinidad is a Technology Evangelist at SAPIEN Technologies Inc., and a Microsoft PowerShell MVP. You can reach him at maxt@sapien.com