September 18th, 2019

Creating a Platform Independent Function in PowerShell

Doctor Scripto
Scripter

Summary: Creating a Function in PowerShell and the power of the $PSVersionTable to make code more portable

Q: Hey, Doctor Scripto!

I saw that post last week on converting Here-String into an array. I wanted to write my code to trap for PowerShell and PowerShell Core. Could you give me a helping hand?

—PG

A: Hello PG,

Hello everyone, your good friend Doctor Scripto is here with Sean sitting across from me having lunch. Sean what do you think about this idea?

Well Doctor Scripto this is actually pretty easy and it makes a LOT of sense to have a function do this to avoid re-writing the same code.

Last week we saw this as a possible solution to convert this Here-String to an array.

$SomeListToUse=@'
Value1
Value2
Value3
Value4
'@

$SomeList=$SomeListToUse -replace "`n" , ''
$SomeList=$SomeList.split("`r")

This works very well. However the challenge as we discovered was if you run this on an environment using PowerShell Core (because of the lack of Linefeeds used) it needed to be re-written this way.

$SomeList=$SomeListToUse.split("`r")

As you can see, the second time we weren’t removing the Linefeed

Now our challenge is this. How to trap for this in code? Our PowerTip last week pointed out that using $PSVersionTable we can identify if we’re running on PowerShell core by watching the PSEdition property.

In our Script we could simply do this as a simple way to make this work in both PowerShell Core and Windows PowerShell.

If ($PSVersionTable.PSedition -eq 'Core')
{
     $SomeList=$SomeListToUse.split("`r")
}
Else
{
     $SomeList=$SomeListToUse -replace "`n" , ''
     $SomeList=$SomeList.split("`r")
}

But what if we need to repeat this process *and* be portable? For this we can use a function to convert our Here-String to an array AND be portable between PowerShell Core and Windows PowerShell.

function ConvertFrom-HereString {
param (
[string]$String=''
)

If ($PSVersionTable.PSEdition -eq 'Core')
     {
          [system.array]$NewArray=$String.split("`r")
     }
else 
     {
          $CleanString=$String -replace "`n" , ''
          [system.array]$NewArray=$CleanString.split("`r")
     }

     return $NewArray

}

We can now, independent of the PowerShell platform type, leverage this function in the same fashion. We can convert Here-String data to an array with this new function.

$HereStringSample=@'
Value1
Value2
Value3
Value4
'@

[system.array]$MyNewArray=ConvertFrom-HereString -string $HereStringSample

 

Just for fun, I tested this on my Windows 10 environment in Windows PowerShell, the Windows Subsystem for Linux (Ubuntu) with PowerShell Core AND PowerShell Core in Windows 10.

PG that is all there is to the basics of writing a Platform independent function in PowerShell.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Forum. See you tomorrow. Until then, peace.

Your good friend, Doctor Scripto

PowerShell, Doctor Scripto, Sean Kearney, Scripting Techniques

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

1 comment

Discussion is closed. Login to edit/delete existing comments.

  • Bill Riedy

    Your function didn’t behave properly with herestrings that have been entered from the Powershell console. I would suggest the following function instead:

    function ConvertFrom-HereString {
    param (
    [string]$String=”
    )
    If ($String.IndexOf(“`r”) -ge 0)
    {
    If ($String.IndexOf(“`n”) -ge 0)
    {
    $String = $String.Replace(“`n”,”)
    [array] $String.Split(“`r”)
    }
    else
    {
    [array] $String.Split(“`r”)
    }
    }
    else
    {
    If ($String.IndexOf(“`n”) -ge 0)
    {
    [array] $String.Split(“`n”)
    }
    else
    {
    [array] ,$String
    }
    }
    } #endfunction