Skip to content

Instantly share code, notes, and snippets.

@blakedrumm
Last active July 16, 2024 05:01
Show Gist options
  • Save blakedrumm/369a040c2b7edf753b37d35f89093967 to your computer and use it in GitHub Desktop.
Save blakedrumm/369a040c2b7edf753b37d35f89093967 to your computer and use it in GitHub Desktop.
Identifies the required modules for a given PowerShell script by analyzing the cmdlets used within the script.
<#
.SYNOPSIS
Identifies the required modules for a given PowerShell script by analyzing the cmdlets used within the script.
.DESCRIPTION
The Get-RequiredModules function takes a script as input, extracts all the cmdlets used in the script, and determines which modules
these cmdlets belong to. It then returns a unique list of required modules.
.PARAMETER Script
The content of the PowerShell script to analyze for required modules.
.NOTES
Ensure that all necessary modules are installed and available in the environment where the script will be run.
.AUTHOR
Blake Drumm (blakedrumm@microsoft.com)
.CREATED
May 21st, 2024
.LINK
Azure Automation Personal Blog: https://blakedrumm.com/
#>
param (
[string]$Script
)
function Get-RequiredModules {
param (
[Parameter(Mandatory = $true)]
[string]$Script
)
# Extract unique cmdlets used in the script
$uniqueCmdlets = [System.Text.RegularExpressions.Regex]::Matches($Script, '\b[A-Za-z0-9]+\-[A-Za-z0-9]+\b') |
ForEach-Object { $_.Value } |
Select-Object -Unique
# Initialize a hash table to store cmdlet to module mapping
$cmdletModuleMap = @{}
# Iterate through each cmdlet to find the corresponding module
foreach ($cmdlet in $uniqueCmdlets) {
try {
$command = Get-Command -Name $cmdlet -ErrorAction Stop
if ($command -and $command.Module -and $command.Module.Name) {
$cmdletModuleMap[$cmdlet] = $command.Module.Name
} else {
Write-Verbose -Verbose "Cmdlet '$cmdlet' not found or has no associated module."
}
} catch {
Write-Verbose -Verbose "Cmdlet '$cmdlet' not found."
}
}
# Get the unique list of required modules
$requiredModules = $cmdletModuleMap.Values | Select-Object -Unique
return $requiredModules
}
if (-not $Script) {
# Example usage:
$scriptContent = @'
# Example Script to show required PowerShell modules
#
# Connect to Azure
Connect-AzAccount
# Get the list of Maintenance Configurations
$maintenanceConfigs = Get-AzMaintenanceConfiguration
# Function to get update management status
function Get-AzVMUpdateManagementStatus {
param (
[string]$ResourceGroupName,
[string]$VMName
)
# Get the VM
$vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName
if ($vm -ne $null) {
# Check the Update Management status
$status = Get-AzUpdateManagement -ResourceGroupName $ResourceGroupName -VMName $VMName
return $status
} else {
Write-Output "VM $VMName not found in resource group $ResourceGroupName"
return $null
}
}
# Loop through each Maintenance Configuration to get the resource status
foreach ($config in $maintenanceConfigs) {
Write-Output "Checking Maintenance Configuration: $($config.Name)"
# Get the resources linked to the Maintenance Configuration
$resources = Get-AzMaintenanceConfigurationAssignment -ResourceGroupName $config.ResourceGroupName -ConfigurationName $config.Name
foreach ($resource in $resources) {
Write-Output "Checking Resource: $($resource.Name)"
# Get the update management status for each resource
$updateStatus = Get-AzVMUpdateManagementStatus -ResourceGroupName $resource.ResourceGroupName -VMName $resource.Name
if ($updateStatus.Status -eq 'Succeeded') {
Write-Output "Updates installed successfully on $($resource.Name)"
} else {
Write-Output "Updates not installed on $($resource.Name)"
}
}
}
'@
} else {
$scriptContent = $Script
}
$requiredModules = Get-RequiredModules -Script $scriptContent
Write-Output "Required Modules: $($requiredModules -join ", ")"
<#
Copyright (c) Microsoft Corporation. MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment