Functions/Validations/Test-Preconditions.ps1
<#
.SYNOPSIS Tests zero or more preconditions for a script to run. .DESCRIPTION Possible preconditions to tests are: 1. If the PowerShell session is 64x or not (x86) 2. If the PowerShell session is elevated with administrator rights. 3. If the PowerShell session runs in FullLanguage mode. 4. If the PowerShell version meets the minimal required version. .EXAMPLE Test-Preconditions -Is64Bit -HasElevatedRights -ValidPowerShellVersion -HasFullLanguage -MinimalMajorVersion 5 -Verbose -ErrorAction Stop #> function Test-Preconditions { [CmdletBinding(DefaultParameterSetName='Default')] param ( # Tests if the PowerShell session is started as a x64 bit process. [switch] $Is64Bit, # Tests if the PowerShell session is started as administrator. [switch] $HasElevatedRights, # Tests if the PowerShell session is running in FullLanguage mode. [switch] $HasFullLanguage, # Tests if the PowerShell version meets the minimal PowerShell version requirements [switch] $ValidPowerShellVersion, # The minimal PS version required to run the task. [Parameter(ParameterSetName='Version', Mandatory=$true)] [version] $MinimalVersion, # The minimal PS major version to run the task. [Parameter(ParameterSetName='Default')] [int] $MinimalMajorVersion = 5, # The minimal PS minor version to run the task. [Parameter(ParameterSetName='Default')] [int] $MinimalMinorVersion = 0 ) $result = @() if($Is64Bit){ $result += Test-BitProcess } if($HasElevatedRights){ $result += Test-ElevatedRights } if($ValidPowerShellVersion){ if($MinimalVersion){ $result += Test-PsVersion -MinimalVersion $MinimalVersion } else { $result += Test-PsVersion -MinimalMajorVersion $MinimalMajorVersion -MinimalMinorVersion $MinimalMinorVersion } } if($HasFullLanguage){ $result += Test-PsLanguageMode } if($false -in $result){ Write-Error 'Not all preconditions have been met to run this task. Resolve the failed precondition(s) before running the task again.' } } Export-ModuleMember -Function Test-Preconditions |