forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
script-azurerm-module-enumerator.ps1
95 lines (78 loc) · 2.92 KB
/
script-azurerm-module-enumerator.ps1
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#script to scan other scripts for azure commands to list needed azure modules
# 190127
[CMDLETBINDING()]
param(
[string]$scriptFile,
[string]$scriptDir,
[ValidateSet('azurerm', 'az')]
[string[]]$module = "azurerm"
)
function main()
{
if(!$scriptDir -and !$scriptFile)
{
$scriptDir = [IO.Path]::GetDirectoryName($MyInvocation.ScriptName)
}
if($scriptFile)
{
$files = @($scriptFile)
}
else
{
$files = [Io.Directory]::GetFiles($scriptDir,"*.ps1",[Io.SearchOption]::TopDirectoryOnly)
}
$neededAzureModules = new-object Collections.ArrayList
$availableModules = (get-module "$module.*" -ListAvailable) | Sort-Object -Property Name,Version -Descending | Get-Unique
foreach($file in $files)
{
write-host "checking file: $($file)"
$scriptSource = Get-Content -Raw -Path $file
$matches = ([regex]::Matches($scriptSource, '\W([a-z]+-azure[a-z]+)\W',[Text.RegularExpressions.RegexOptions]::IgnoreCase))
foreach($match in ($matches| Sort-Object -Unique))
{
$matchedValue = $match.Captures[0].Groups[1].Value
write-host "`t`tazure command: $($matchedValue)"
$module = $null
foreach($availableModule in $availableModules)
{
write-verbose "`t`t`tavailable module: $($availableModule)"
if($availableModule.ExportedCmdlets.Keys -imatch $matchedValue `
-or $availableModule.ExportedAliases.Keys -imatch $matchedValue)
{
$module = $availableModule.Name
if($module)
{
write-verbose "`t`t`t`tchecking if module in list: $($module)"
if(!$neededAzureModules.Contains($module))
{
write-host "`t`t`t`t`tadding module to list: $($module)" -ForegroundColor Green
[void]$neededAzureModules.Add($module)
continue
}
else
{
write-host "`t`t`t`t`tmodule already in list: $($module)" -ForegroundColor DarkGray
}
}
}
}
if(!$module)
{
Write-Warning "unable to find module for command $($matchedValue)"
}
}
if($neededAzureModules.Count -gt 0)
{
write-host "needed modules for file: $($file)"
write-host "$($neededAzureModules | format-list * | out-string)" -ForegroundColor Magenta
}
else
{
write-host "NO azure modules needed for file: $($file)" -ForegroundColor Gray
}
$neededAzureModules.Clear()
write-host "-----------------------------------------"
}
$neededAzureModules
}
main