Posey's Tips & Tricks

How To Create Cross-Platform PowerShell Scripts

To avoid errors, it's important to write PowerShell scripts that prevent code from running on an unintended platform. Luckily, this is easier to do than it sounds.

Up until a few years ago, PowerShell scripts could only run on Windows machines. Admittedly, that sounds like a really obvious and inconsequential statement, but think about what it means: Back then, it was possible to create a PowerShell script without thinking about what kind of machine the script would eventually run on.

Today, this simply is not the case. A PowerShell script that is intended for a Windows machine probably isn't going to work correctly on a Linux machine. Of course, the opposite is also true.

Actually, it's a little bit worse than that. If you attempt to run a PowerShell script on a different platform than it was intended for, some of the commands within the script will probably work, while others are unsupported and will therefore generate errors. The end result will be completely unpredictable. In some cases, the script could end up doing damage.

That being the case, it is important to build PowerShell scripts that are either cross-platform, or that prevent code from running on an unintended platform. Believe it or not, this is easier to do than it sounds.

Let me start with a really simple script that does hardly anything. I will then build on this script as I introduce more concepts. Here is the script:

Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
}

RunOn-Windows

This script creates a function called RunOn-Windows. As I previously noted, the function doesn't really do much. It simply displays a message indicating that the script is running on a Windows machine. The script's body is equally simple. It contains a single command, which calls the function.

As it is written, this script has no idea if it is running on a Windows machine or not. But what if it were possible to perform a test to determine whether the script is running on Windows, and call the function only if the Windows operating system is detected? It's not that hard to do, but you will need to make sure that you are running PowerShell version 6 or higher. Otherwise, this technique won't work.

Microsoft provides a series of variables that you can use to see what operating system is being used. If you want to see if a script is running on Windows, for instance, you can use the $IsWindows variable. This variable is set to True if the system is Windows-based, or False if the operating system is something else. We can easily evaluate the variable as a part of the function call. Here is what the script might look like:

Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
}

If ($IsWindows) {RunOn-Windows}

As you can see, I haven't touched the function itself. What I have done, however, reconstruct the function call so that it only executes if the $IsWindows variable is true. This is a great way to prevent Windows code from running on a non-Windows system.

But what if you want to inform users of non-Windows systems that the script only works on Windows? The only thing we have to do is add an Else statement. Here is what such a statement might look like:

If ($IsWindows) {RunOn-Windows} else {Write-Host 'This script only works on Windows computers'}

This line of code checks if the script is running on Windows. If it is running on Windows, then it calls the RunOn-Windows function. If the script is running on another platform, it displays a message indicating that the script only works on Windows systems.

As handy as this technique might be for preventing a script from running on an unintended platform, it can actually be extended to make a script multiplatform. Here is what such a script might look like:

Function RunOn-Windows
{
Write-Host 'The Script is Running on a Windows Machine'
}

Function RunOn-Linux
{
Write-Host 'The Script is Running on a Linux Machine'
}

Function RunOn-Mac
{
Write-Host 'The Script is Running on a Mac'
}

If ($IsWindows) {RunOn-Windows}
If ($IsLinux) {RunOn-Linux}
If ($IsMacOS) {RunOn-Mac}

This script contains three different functions. Remember, a function never executes unless it is explicitly called. One function contains code that is intended for use on a Windows machine, while the other functions contain code for Mac and Linux.

The script's main body contains three If statements. If the script is found to be running on a Windows machine, it calls a function containing a Windows version of the code. If the script is running on a Linux machine, it calls a function containing Linux-specific commands. Likewise, if the script is running on a Mac, then the script will call a function containing code that is intended for use on a Mac.

About the Author

Brien Posey is a 19-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.

Featured

  • Weird Blue Tunnel Graphic

    Microsoft Goes Deep on 'Solorigate' Secondary Attack Methods

    Microsoft on Wednesday published an analysis of the second-stage "Solorigate" attack methods used by an advanced persistent threat (APT) attack group.

  • Microsoft Talks Teams and SharePoint at Modern Workplace Event

    It's a hybrid world, but remote work is here to stay, according to Microsoft's Teams and SharePoint head Jeff Teper.

  • Malwarebytes Affirms Other APT Attack Methods Used Besides 'Solorigate'

    Security solutions company Malwarebytes affirmed on Monday that alternative methods besides tainted SolarWinds Orion software were used in the recent "Solorigate" advanced persistent threat (APT) attacks.

  • How To Fix the Hyper-V Read Only Disk Problem

    DOS might seem like a relic now, but sometimes it's the only way to fix a problem that Windows seems ill-equipped to deal with -- like this one.

comments powered by Disqus