mardi 22 mars 2016

PowerShell Version check prior to script execution

I'm just asking the communitty if they have come across a way to have the a script check what version of POSH is running prior to the execution of the script. Currently, my work around is the following code:

    #region Final Checks

    #//Check to make sure that version of PowerShell is at least 3.0 before preceding.

    If($PSVersionTable.PSVersion.Major -le 2) {
        Throw "This script has not been tested with version 2.0 or older of PowerShell.  Please execute this script from a system that has PowerShell 3.0 or newer installed.  Windows 8/Server 2012 and newer has it installed by default.  Windows 7/Server 2008 R2 can be patched to have 3.0 installed."
        }

    #endregion Final Checks

I have this right after defining my parameters. However, for my own crazy sake, I want the script to preform this check automatically prior to getting into the meat and potato of the script. A good comparison is using Validate[X] for a parameter. If an operator tries to provide data that doesn't fit my user, an error is thrown prior to the execution of the script. Any ideas? I know that there is nothing in [CmdletBinding()] that does it. Thanks!



PowerShell Version check prior to script execution

Aucun commentaire:

Enregistrer un commentaire