Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/matsest/get-az-modules

Identify exactly which Az modules you need to run your script
https://github.com/matsest/get-az-modules

az azure azure-powershell dependencies dependencies-checking powershell pwsh

Last synced: about 2 months ago
JSON representation

Identify exactly which Az modules you need to run your script

Awesome Lists containing this project

README

        

# 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](https://github.com/Azure/azure-powershell/) 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](https://docs.microsoft.com/en-us/powershell/azure/install-az-ps).

## Usage

```powershell
./Get-AzModules.ps1 -Path
```

### Examples

Tips: Try to run the script against any of the files in [examples](./examples).

#### Basic usage

```powershell
./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
```powershell
./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

```powershell
/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

```powershell
./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

```powershell
$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
```powershell
./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](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-7.4#-modules-module-name--hashtable) statement.

```powershell
#Requires -Modules @{ ModuleName=""; ModuleVersion="" }
```

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`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/import-module?view=powershell-7.4) explicitly with versions specified:

```powershell
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](./LICENSE).