The Verify-Prerequisites
function checks and installs the required modules and tools for the main script. This is essential for verifying whether all prerequisites are installed, available, and up to date, which minimizes the chance of errors during script execution.
What exactly does the function do?
Check and install modules:
- The function defines a list of required modules, such as
Az
andMicrosoft.Graph
. - For each module, the function checks whether it is already installed using
Get-InstalledModule
. - If a module is not installed, it uses
Install-Module
to install it. - If the module is not the latest version, it updates it using
Update-Module
.
Verify Azure CLI:
- The function checks if the Azure CLI is installed using
Get-Command az
. - If the Azure CLI is not installed, a message is displayed with instructions on how to install it.
Check Winget:
- The function checks whether Winget is installed, except on Windows Server.
- If Winget is installed, it performs the check.
- If Winget is not installed, a message is shown with instructions on how to install it.
Set global variable:
- The function sets the global variable
$global:installed
to$true
to indicate that all prerequisites have been checked and installed.
This prevents the installation process from running again when the script is executed a second time in the same context.
Why is verifying that all prerequisites are installed useful?
Running this function in advance ensures that all required modules and tools are present and up to date before the main script is executed. This prevents interruptions and errors during script execution. Updating the Az module can sometimes take a long time, as you may have experienced. Running this function beforehand reduces such delays and improves the overall efficiency of the script.
Here is the sample script:
# Begin Function Verify-Prerequisites
Function Verify-Prerequisites {
$modules = @(
@{ Name = "Az" },
@{ Name = "Microsoft.Graph" }
)
if (-not $global:installed) {
foreach ($module in $modules) {
$installedModule = Get-InstalledModule -Name $module.Name -ErrorAction SilentlyContinue
if ($null -eq $installedModule) {
Write-Host "$($module.Name) is not installed. Installing the latest version..."
Install-Module -Name $module.Name -Force -Scope CurrentUser
} else {
$latestModule = Find-Module -Name $module.Name
if ($installedModule.Version -lt $latestModule.Version) {
Write-Host "$($module.Name) is installed but not the latest version. Updating to version $($latestModule.Version)..."
Update-Module -Name $module.Name -Force
} else {
Write-Host "$($module.Name) is installed and up-to-date. Version: $($installedModule.Version)"
}
}
}
# Check if Azure CLI is installed
if (Get-Command az -ErrorAction SilentlyContinue) {
Write-Host "Azure CLI is installed."
} else {
Write-Host "Azure CLI is not installed. Please install it from https://learn.microsoft.com/nl-nl/cli/azure/install-azure-cli-windows?pivots=msi"
}
# Check if Winget is installed (only if not on Windows Server)
$osCaption = (Get-WmiObject -Class Win32_OperatingSystem).Caption
if ($osCaption -match "Windows Server") {
Write-Host "Skipping Winget check on Windows Server."
} else {
if (Get-Command winget -ErrorAction SilentlyContinue) {
$wingetVersion = winget --version 2>&1 | Out-String
if ($wingetVersion -match "v(\d+\.\d+\.\d+)") {
Write-Host "Winget is installed. Version: $($matches[1])"
} else {
Write-Host "Winget is installed but version could not be determined."
}
} else {
Write-Host "Winget is not installed. Please install it from https://aka.ms/GetWinget"
}
}
$global:installed = $true
Write-Host "Installation was successful. All prerequisites met. No further action needed."
} else {
Write-Host "All prerequisites met. No further action needed."
}
}
# End Function Verify-Prerequisites
More information about PowerShell can be found here. An example how to use PowerShell in a real live situations can be found here.