PowerShell Open Source – Take advantage of ‘#Requires -Version’ and new variables

Now that PowerShell Open Source is evolving, we need taking advantage of some if it features to build cross-platform scripts. Also, a good way to test the script functionality is to use either Linux or Windows 10 Bash environments.

Just keep in mind, for now you won’t be able to run you existing Windows PowerShell scripts or modules in another OS. Unless, you concentrate that write your scripts only using the PowerShell Core module. PowerShell Open Source is evolving.

Also, there are already modules available that can be use cross-platform thru PowerShell Gallery, and NuGet. Just make sure to search of Linux or Mac OS scripts.

[sourcecode language=”powershell”]
Find-Module -tag linux
[/sourcecode]

poshadv_01_2016-11-26_11-35-07

Using PowerShell existing features

Now, when creating cross-platform scripts, I found very useful to have the “ #Requires -Version x” statement. This statement is available in all versions of PowerShell.

Next, is to take advantage of the new PowerShell Open Source variables:
– IsCoreCLR
– IsLinux
– IsOSX
– IsWindows

poshadv_02_2016-11-26_11-35-07

By just adding a conditional statement (“if”) to check which OS environment you are running the script. This makes it so convenient and portal across multiple OS environments.

Sample Script

To demostrate, *here’s a sample script for searching Debian packages I use on my Window 10 and Linux systems:

[sourcecode language=”powershell”]
<# .NOTES =========================================================================== Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.129 Created on: 10/31/2016 2:18 PM Created by: Maximo Trinidad Organization: PutItTogether Filename: CrossPlatTest.ps1 =========================================================================== .DESCRIPTION A description of the file. #>

#requires -version 6
function Get-DebianFiles
{
Param (
[string]$FileExtension = “*.deb”
)
write-host “Only in PowerShell 6.0”
if ($IsWindows -eq $true)
{
$PackageLocation = ‘C:\Users\mtrinidad\Downloads’;
Write-Verbose “Windows Selected” -Verbose;
};

if($IsLinux -eq $true)
{
$PackageLocation = ‘/mnt/c/Users/mtrinidad/Downloads’;
Write-Verbose “BASH Selected” -Verbose;
};

Get-ChildItem $PackageLocation -File -Filter $FileExtension -Recurse;
#exit
};

#. C:\TempPoshXplat\Scripts\PowerShell\CrossPlatTest.ps1

## Get-DebianFiles -FileExtension “*.deb”;
Get-DebianFiles -FileExtension “*.deb”;

## ———- end of scripts ———–##
[/sourcecode]

*Note: In order for the ” #Requires -Version..” statement to work, it need to execute from a script file *.ps1. The statement won’t work from inside a function.

The above script is required to run in PowerShell 6 because is using both new variables: IsWindows and IsLinux. Keep in mind, this sample have hardcoded path to where I’ve stored my debian packages and it can be improved more.

poshadv_04_2016-11-26_11-35-07

poshadv_03_2016-11-26_11-35-07

There still a lot of work to be done in both Windows 10 Bash and PowerShell Open Source. The variables to properly identify the OS in non-Windows system hasn’t been implemented yet. But, it will be available sometime soon.

In the meantime, keep exploring, testing, and contributing in Github!
https://github.com/PowerShell/PowerShell