-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitialize-ScriptPrerequisites.ps1
More file actions
134 lines (109 loc) · 4.57 KB
/
Copy pathInitialize-ScriptPrerequisites.ps1
File metadata and controls
134 lines (109 loc) · 4.57 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<#
.SYNOPSIS
Checks and optionally installs prerequisites for this scripts repository.
.DESCRIPTION
Validates required and recommended tools/modules used across repository scripts.
By default, it runs in check-only mode and returns a status report.
.PARAMETER InstallMissingModules
Installs missing PowerShell modules (Az, Microsoft.Graph, Pester) in CurrentUser scope.
.PARAMETER InstallAzureCli
Installs Azure CLI using winget if missing and winget is available.
.EXAMPLE
Initialize-ScriptPrerequisites.ps1
.EXAMPLE
Initialize-ScriptPrerequisites.ps1 -InstallMissingModules -InstallAzureCli
#>
[CmdletBinding()]
param(
[Parameter()]
[switch]$InstallMissingModules,
[Parameter()]
[switch]$InstallAzureCli
)
function New-StatusRecord {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[string]$Name,
[Parameter(Mandatory)]
[string]$Type,
[Parameter(Mandatory)]
[bool]$Required,
[Parameter(Mandatory)]
[bool]$Installed,
[Parameter()]
[string]$Version = "",
[Parameter()]
[string]$InstallCommand = ""
)
[pscustomobject]@{
Name = $Name
Type = $Type
Required = $Required
Installed = $Installed
Version = $Version
InstallCommand = $InstallCommand
}
}
$results = New-Object System.Collections.Generic.List[object]
$pwshCompliant = $PSVersionTable.PSVersion -ge [version]"7.0.0"
$results.Add((New-StatusRecord -Name "PowerShell 7+" -Type "Runtime" -Required $true -Installed $pwshCompliant -Version $PSVersionTable.PSVersion.ToString() -InstallCommand "https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell"))
$requiredCommands = @(
@{ Name = "git"; Required = $true; InstallCommand = "https://git-scm.com/downloads" },
@{ Name = "winget"; Required = $false; InstallCommand = "https://learn.microsoft.com/en-us/windows/package-manager/winget/" },
@{ Name = "choco"; Required = $false; InstallCommand = "https://chocolatey.org/install" },
@{ Name = "docker"; Required = $false; InstallCommand = "https://www.docker.com/products/docker-desktop/" },
@{ Name = "az"; Required = $false; InstallCommand = "https://learn.microsoft.com/en-us/cli/azure/install-azure-cli" },
@{ Name = "kubectl"; Required = $false; InstallCommand = "https://kubernetes.io/docs/tasks/tools/" }
)
foreach ($commandInfo in $requiredCommands) {
$command = Get-Command $commandInfo.Name -ErrorAction SilentlyContinue
$version = ""
if ($command) {
$version = $command.Version.ToString()
}
$results.Add((
New-StatusRecord `
-Name $commandInfo.Name `
-Type "Command" `
-Required $commandInfo.Required `
-Installed ([bool]$command) `
-Version $version `
-InstallCommand $commandInfo.InstallCommand
))
}
$moduleChecks = @(
@{ Name = "Az"; Required = $false; InstallCommand = "Install-Module Az -Scope CurrentUser -Force" },
@{ Name = "Microsoft.Graph"; Required = $false; InstallCommand = "Install-Module Microsoft.Graph -Scope CurrentUser -Force" },
@{ Name = "Pester"; Required = $false; InstallCommand = "Install-Module Pester -Scope CurrentUser -Force -SkipPublisherCheck" }
)
foreach ($moduleInfo in $moduleChecks) {
$module = Get-Module -ListAvailable -Name $moduleInfo.Name | Sort-Object Version -Descending | Select-Object -First 1
$moduleVersion = ""
if ($module) {
$moduleVersion = $module.Version.ToString()
}
$results.Add((
New-StatusRecord `
-Name $moduleInfo.Name `
-Type "Module" `
-Required $moduleInfo.Required `
-Installed ([bool]$module) `
-Version $moduleVersion `
-InstallCommand $moduleInfo.InstallCommand
))
if ($InstallMissingModules -and -not $module) {
switch ($moduleInfo.Name) {
"Az" { Install-Module Az -Scope CurrentUser -Force; break }
"Microsoft.Graph" { Install-Module Microsoft.Graph -Scope CurrentUser -Force; break }
"Pester" { Install-Module Pester -Scope CurrentUser -Force -SkipPublisherCheck; break }
}
}
}
$azCheck = $results | Where-Object { $_.Name -eq "az" }
$wingetCheck = $results | Where-Object { $_.Name -eq "winget" }
if ($InstallAzureCli -and -not $azCheck.Installed -and $wingetCheck.Installed) {
winget install --id Microsoft.AzureCLI --source winget --accept-package-agreements --accept-source-agreements
}
$results | Sort-Object Type, Name | Format-Table -AutoSize
$results