Example of PowerShell Main base script with detection of PowerShell version and Azure PowerShell version.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
param( [switch] $Help = $false ) function Usage() { Write-Host 'Expected parameters:' Write-Host ' -Help : Shows this help.' exit 0 } function DetectPowershellVersion($requiredVersion) { $PSVersion = [int] $PSVersionTable.PSVersion.Major Write-Host "Detected Powershell Version $PSVersion (Required is $requiredVersion)" if ($PSVersion -lt $requiredVersion) { throw "Wrong Powershell Version $PSVersion (Required is $requiredVersion)" } } function DetectAzurePowershellVersion() { $AzurePSVersion = (Get-Module -ListAvailable -Name Azure -Refresh).Version.ToString() Write-Host "Detected Azure Powershell Version $AzurePSVersion" } # Global variables $scriptDir = Split-Path ($MyInvocation.MyCommand.Definition) -parent Set-Location $scriptDir #Main try { #Preparation steps $startTime = Get-Date Write-Host -ForegroundColor Cyan "Script started ($startTime)" DetectPowershellVersion 4 DetectAzurePowerShellVersion #1 Write-Host -ForegroundColor Cyan "Step 1: ..." Write-Host Write-Host # END $endTime = Get-Date Write-Host "Script finished ($endTime)" $elapsedTime = new-timespan $startTime $endTime Write-Host -ForegroundColor Cyan "SUCCESS: Script time = $elapsedTime" exit 0 } catch [Exception] { Write-Host -ForegroundColor Magenta $psItem.Exception Write-Host -ForegroundColor Magenta $psItem.ErrorDetails Write-Error -Exception $psItem.Exception Exit 1 } |