Creating a Platform Independent Function in PowerShell

Doctor Scripto

Dr Scripto

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.

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.

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.

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.

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.

 

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

Doctor Scripto
Dr Scripto

Scripter, PowerShell, vbScript, BAT, CMD

Follow Dr Scripto   

1 comment

  • Avatar
    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

Leave a comment