Skip to content

Identify exactly which Az modules you need to run your script

License

Notifications You must be signed in to change notification settings

matsest/get-az-modules

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Get Az Modules

Description

A Powershell script that identifies which Az modules is needed to run a script that contains Az cmdlets. Can also be used to identify required Az modules for your custom module.

Avoid installing all Az.* modules in GitHub runners, Azure Automation accounts and other transient/minimal environments to save time and resources and handle your dependencies more explicitly.

Why?

  1. Only use the depencies you need (only use required modules)
  2. Deal with dependencies explicitly (use required versions)

PS: If you're new to the Az module please refer to the official installation docs.

Usage

./Get-AzModules.ps1 -Path <path to .ps1 or .psm1 file>

Examples

Tips: Try to run the script against any of the files in examples.

Basic usage

./Get-AzModules.ps1 -Path ./examples/sample.ps1

Looking for Az modules needed for sample.ps1...
Number of unique Az cmdlets found: 17
Number of Az modules used: 3
Az.Compute
Az.Network
Az.Resources

Check versions of installed modules

./Get-AzModules.ps1 -Path ./examples/sample.ps1 -CheckVersions

Looking for Az modules needed for sample.ps1...
Number of unique Az cmdlets found: 17
Number of Az modules used: 3
WARNING: Az.Resources: latest version [4.3.1] newer than installed version [4.2.0]
WARNING: Az.Compute: latest version [4.17.0] newer than installed version [4.15.0]
WARNING: Az.Network: latest version [4.11.0] newer than installed version [4.10.0]

Name         InstalledVersion LatestVersion
----         ---------------- -------------
Az.Resources 4.2.0            4.3.1
Az.Compute   4.15.0           4.17.0
Az.Network   4.10.0           4.11.0

Get info about modules not installed

/Get-AzModules.ps1 -Path ./examples/nondefault.ps1

Looking for Az modules needed for nondefault.ps1...
Number of unique Az cmdlets found: 3
WARNING: Get-AzSubscriptionAlias was not found. Available in module Az.Subscription [0.8.0] from PSGallery
WARNING: Get-AzStackEdgeDevice was not found. Available in module Az.StackEdge [0.1.0] from PSGallery
Number of Az modules used: 3
Az.Resources
Az.StackEdge
Az.Subscription

Print all cmdlets

./Get-AzModules.ps1 -Path ./examples/sample.ps1 -Verbose

Looking for Az modules needed for sample.ps1...
Number of unique Az cmdlets found: 17
VERBOSE: New-AzResourceGroup uses Az.Resources
VERBOSE: New-AzAvailabilitySet uses Az.Compute
VERBOSE: New-AzVirtualNetworkSubnetConfig uses Az.Network
VERBOSE: New-AzVirtualNetwork uses Az.Network
VERBOSE: New-AzPublicIpAddress uses Az.Network
VERBOSE: New-AzLoadBalancerFrontendIpConfig uses Az.Network
VERBOSE: New-AzLoadBalancerBackendAddressPoolConfig uses Az.Network
VERBOSE: New-AzLoadBalancerProbeConfig uses Az.Network
VERBOSE: New-AzLoadBalancerRuleConfig uses Az.Network
VERBOSE: New-AzLoadBalancer uses Az.Network
VERBOSE: New-AzNetworkInterfaceIpConfig uses Az.Network
VERBOSE: New-AzNetworkInterface uses Az.Network
VERBOSE: New-AzVMConfig uses Az.Compute
VERBOSE: Set-AzVMOperatingSystem uses Az.Compute
VERBOSE: Set-AzVMSourceImage uses Az.Compute
VERBOSE: Add-AzVMNetworkInterface uses Az.Compute
VERBOSE: New-AzVM uses Az.Compute
Number of Az modules used: 3
Az.Compute
Az.Network
Az.Resources

Use in script or pipeline

$modules = ./Get-AzModules.ps1 -Path ./examples/sample.ps1 -CheckVersions

foreach ($module in $modules){
    Install-Module $module.Name -RequiredVersion $module.LatestVersion -Force
}

Check against other modules than Az

./Get-AzModules.ps1 -Path ./examples/sample-mg.ps1 -Prefix Mg

Looking for Mg modules needed for sample-mg.ps1...
Number of unique Mg cmdlets found: 3
Number of Mg modules used: 3
Microsoft.Graph.Authentication
Microsoft.Graph.Teams
Microsoft.Graph.Users

Dealing with dependencies in scripts

Using #requires

In the beginning of your script you can add the #requires statement.

#Requires -Modules @{ ModuleName="<modulename>"; ModuleVersion="<version>" }

This will need to be added for each module you want to use. Note that ModuleVersion only specifies the minimum acceptable version of the module. To require an exact, required version of a module, replace ModuleVersion with RequiredVersion.

Using Import-Module

An alternative is to in your script, or in a separate script, run Import-Module explicitly with versions specified:

Import-Module -Name Az.Resources -RequiredVersion 4.3.1 # specify exact version
Import-Module -Name Az.Resources -MinimumVersion 4.3.0 # specify minimum version
Import-Module -Name Az.Resources -MaximumVersion 4.4.0 # specify maximum version

You can also add -ErrorAction Stop to the cmdlets or set $ErrorActionPreference="Stop" in your script to ensure the script ensures the cmdlets run without errors.

License

The MIT License applies to the code contained in this repo. For more information, see LICENSE.