SAPIEN Tools – How-To Work with PowerShell Core in PrimalScript

Now that PowerShell Core 6.0.0 has reached the GA (Generally Available) status, you can start developing scripts using PrimalScript.

HowToPSCore_01_2018-01-16_9-40-55

Yes, in this product look under “File | Options“, in the “Environment” section you’ll find the “Window Command” area. Here, PowerShell Core shell can be added manually.  Also, if you’re using Windows 10 Build 16299 (or higher) with WSL (Windows Subsystem for Linux), your system can also be setup to run both “Bash” and “Bash with PowerShell Core“.

HowToPSCore_01a_2018-01-16_9-40-55

Let’s think about this for a minute. Although our SAPIEN Products are meant to be used in Windows Systems, having WSL installed will give you the ability to build and test PowerShell Core scripts cross-platform without leaving Windows.

Take a moment to look at the existing blog post:
https://www.sapien.com/blog/2017/09/15/using-powershell-core-in-primalscript/

For more information about WSL (Windows Subsystem for Linux) click the following link:
https://blogs.msdn.microsoft.com/commandline/

 

Setting up PowerShell Core in PrimalScript

The following steps will properly setup PowerShell Core in PrimalScript:

1. Under the “File” menu, go to “Options“.
2. In the “Environment” section, click on “Command Window“.
3. Add a new shell by clicking on the “New” button. This will add a new line to be changed.

*Note: Keep in mind, this could be any type of console shell, such as: Python, Ruby, Bash.

HowToPSCore_02_2018-01-16_9-40-55

HowToPSCore_03_2018-01-16_9-40-55

Now, we add PowerShell Core to the “Command Window” list. In the “New Shell” line, do the following:

A. Double-Click on the “New Shell” line, and it will allow you to search for the shell executable,

HowToPSCore_04_2018-01-16_9-40-55

Or, simply type the following line:

C:\Program Files\PowerShell\6.0.0\pwsh.exe -noexit -Command Remove-Module PSReadline -ErrorAction Ignore

HowToPSCore_06_2018-01-16_9-40-55

B. Now, click the “Rename” button to change the “Shell Name” field from “New Shell” to “PowerShell Core 64bit“:

HowToPSCore_05_2018-01-16_9-40-55

C. To complete the change, click the “OK” button and restart PrimalScript.

HowToPSCore_07_2018-01-16_9-40-55

To verify the shell has been added to the “Console Window” list, go to “View” and click on the “Shell” option, then click the arrow-down to view the list.

HowToPSCore_08_2018-01-16_9-40-55

HowToPSCore_09_2018-01-16_9-40-55

 

Make PowerShell Core the Active Console Window

To activate the added shell, look for the “Console Window” panel and click on the pull-down list of available shells.

Proceed to select “PowerShell Core 64bit” shell and it will become active in PrimalScript. Then, type “$PSVersionTable” to check that the PowerShell version is PowerShell Core 6.0.0.

HowToPSCore_10_2018-01-16_9-40-55

If there is any need to reset the console, this can be done by right-clicking on the shell and selecting  “Reset“.

HowToPSCore_12_2018-01-16_9-40-55

 

Testing your PowerShell Core Scripts

There are a couple of ways to test your script(s):

1. Just highlight and select a few lines of the code, then right-click and select “Execute selection in PowerShell“.

HowToPSCore_13_2018-01-16_9-40-55

2. Or, to execute the whole script, from the “Home” menu click on the “Script Run – Run* button, then select either “Run in Shell” or “Run Selection in Shell“.

HowToPSCore_13a_2018-01-16_9-40-55

The script will run under the active console whether it is PowerShell Core or any other shell.

*Note: If you select to just “Run” the script, it will think you are executing a script for non-Powershell Core.  This may give an error, but depends on how the script was built.

 

Some Caveats

Testing PowerShell Core scripts is doable! But, here’s the catch: PowerShell and PowerShell Core share the same core modules with slight differences, such as a few parameters changed and/or a very few non-supported cmdlets in PowerShell Core (for example: Out-GridView).

HowToPSCore_14_2018-01-16_9-40-55

So, when using PrimalScript to develop PowerShell Core, test your scripts using the proper “Run Shell” option. At the same time, understand the difference between the two versions of PowerShell.

 

Upcoming Improvements

SAPIEN Technologies, Inc. is dedicated to providing the best scripting technology tools for the IT and DevOP professionals.

Stay tuned for more!