Refactor functions for improved readability

This commit is contained in:
Zehadi Alam
2024-03-31 16:12:22 -04:00
parent 996d4352fd
commit c3a4da7914
+53 -83
View File
@@ -531,54 +531,49 @@ function Install-ADK {
throw $_ throw $_
} }
} }
function Find-InstalledProgramInfo { function Get-InstalledProgramRegKey {
param ( param (
[array]$UninstallRegKeys, [string]$DisplayName
[string]$RegValueNameFilter,
[string]$RegValueDataFilter,
[string]$RegValueDataRetrieve
) )
foreach ($key in $UninstallRegKeys) { $uninstallRegPath = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$uninstallRegKeys = Get-ChildItem -Path $uninstallRegPath -Recurse
foreach ($regKey in $uninstallRegKeys) {
try { try {
$value = $key.GetValue($RegValueNameFilter) $regValue = $regKey.GetValue("DisplayName")
# Check if the value matches the provided filter if ($regValue -eq $DisplayName) {
if ($value -eq $RegValueDataFilter) { return $regKey
$installedProgramInfo = $key.GetValue($RegValueDataRetrieve)
return $installedProgramInfo
} }
} }
catch { catch {
WriteLog $_ WriteLog $_
throw "Error occurred while retrieving info for $RegValueDataFilter." throw "Error retrieving installed program info for $DisplayName : $_"
} }
} }
} }
function Uninstall-ADK { function Uninstall-ADK {
param ( param (
[ValidateSet("Windows ADK", "WinPE add-on")] [ValidateSet("Windows ADK", "WinPE add-on")]
[string]$ADKOption, [string]$ADKOption
[array]$UninstallRegKeys
) )
# Match name as it appears in the registry # Match name as it appears in the registry
$displayName = @{ $displayName = switch ($ADKOption) {
"Windows ADK" = "Windows Assessment and Deployment Kit" "Windows ADK" { "Windows Assessment and Deployment Kit" }
"WinPE add-on" = "Windows Assessment and Deployment Kit Windows Preinstallation Environment Add-ons" "WinPE add-on" { "Windows Assessment and Deployment Kit Windows Preinstallation Environment Add-ons" }
}[$ADKOption] }
try { try {
$adkBundleCachePath = Find-InstalledProgramInfo ` $adkRegKey = Get-InstalledProgramRegKey -DisplayName $displayName
-UninstallRegKeys $UninstallRegKeys `
-RegValueNameFilter "DisplayName" `
-RegValueDataFilter "$displayName" `
-RegValueDataRetrieve "BundleCachePath"
if (-not $adkBundleCachePath) { if (-not $adkRegKey) {
WriteLog "$ADKOption is not installed." WriteLog "$ADKOption is not installed."
return return
} }
$adkBundleCachePath = $adkRegKey.GetValue("BundleCachePath")
WriteLog "Uninstalling $ADKOption..." WriteLog "Uninstalling $ADKOption..."
Invoke-Process $adkBundleCachePath "/uninstall /quiet" Invoke-Process $adkBundleCachePath "/uninstall /quiet"
WriteLog "$ADKOption uninstalled successfully." WriteLog "$ADKOption uninstalled successfully."
@@ -589,46 +584,41 @@ function Uninstall-ADK {
throw $_ throw $_
} }
} }
function Confirm-ADKVersionIsLatest { function Confirm-ADKVersionIsLatest {
param ( param (
[ValidateSet("Windows ADK", "WinPE add-on")] [ValidateSet("Windows ADK", "WinPE add-on")]
[string]$ADKOption, [string]$ADKOption
[array]$UninstallRegKeys
) )
# Match name as it appears in the registry $displayName = switch ($ADKOption) {
$displayName = @{ "Windows ADK" { "Windows Assessment and Deployment Kit" }
"Windows ADK" = "Windows Assessment and Deployment Kit" "WinPE add-on" { "Windows Assessment and Deployment Kit Windows Preinstallation Environment Add-ons" }
"WinPE add-on" = "Windows Assessment and Deployment Kit Windows Preinstallation Environment Add-ons" }
}[$ADKOption]
try { try {
# Get installed ADK version $adkRegKey = Get-InstalledProgramRegKey -DisplayName $displayName
$installedADKVersion = Find-InstalledProgramInfo `
-UninstallRegKeys $UninstallRegKeys `
-RegValueNameFilter "DisplayName" `
-RegValueDataFilter "$displayName" `
-RegValueDataRetrieve "DisplayVersion"
if (-not $installedADKVersion) { if (-not $adkRegKey) {
WriteLog "Failed to get $ADKOption version. It may not be installed."
return $false return $false
} }
# Retrieve content of Microsoft documentation page $installedADKVersion = $adkRegKey.GetValue("DisplayVersion")
$ADKWebPage = Invoke-RestMethod "https://learn.microsoft.com/en-us/windows-hardware/get-started/adk-install"
# Specify regex pattern for ADK version
$ADKVersionPattern = 'ADK\s+(\d+(\.\d+)+)'
# Check for regex pattern match
$ADKVersionMatch = [regex]::Match($ADKWebPage, $ADKVersionPattern)
if (-not $ADKVersionMatch.Success) { # Retrieve content of Microsoft documentation page
$adkWebPage = Invoke-RestMethod "https://learn.microsoft.com/en-us/windows-hardware/get-started/adk-install"
# Specify regex pattern for ADK version
$adkVersionPattern = 'ADK\s+(\d+(\.\d+)+)'
# Check for regex pattern match
$adkVersionMatch = [regex]::Match($adkWebPage, $adkVersionPattern)
if (-not $adkVersionMatch.Success) {
WriteLog "Failed to retrieve latest ADK version from web page." WriteLog "Failed to retrieve latest ADK version from web page."
return $false return $false
} }
# Extract ADK version from the matched pattern # Extract ADK version from the matched pattern
$latestADKVersion = $ADKVersionMatch.Groups[1].Value $latestADKVersion = $adkVersionMatch.Groups[1].Value
if ($installedADKVersion -eq $latestADKVersion) { if ($installedADKVersion -eq $latestADKVersion) {
WriteLog "Installed $ADKOption version $installedADKVersion is the latest." WriteLog "Installed $ADKOption version $installedADKVersion is the latest."
@@ -646,44 +636,32 @@ function Confirm-ADKVersionIsLatest {
} }
function Get-ADK { function Get-ADK {
# Define registry paths
$uninstallRegPath = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
$adkRegKey = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots"
$adkRegValueName = "KitsRoot10"
# Get all uninstall registry keys
$uninstallRegKeys = Get-ChildItem -Path $uninstallRegPath -Recurse
$uninstallKeysUpdated = $false
# Check if latest ADK and WinPE add-on are installed # Check if latest ADK and WinPE add-on are installed
$latestADKInstalled = Confirm-ADKVersionIsLatest -ADKOption "Windows ADK" -UninstallRegKeys $uninstallRegKeys $latestADKInstalled = Confirm-ADKVersionIsLatest -ADKOption "Windows ADK"
$latestWinPEInstalled = Confirm-ADKVersionIsLatest -ADKOption "WinPE add-on" -UninstallRegKeys $uninstallRegKeys $latestWinPEInstalled = Confirm-ADKVersionIsLatest -ADKOption "WinPE add-on"
# Uninstall older versions and install latest versions if necessary # Uninstall older versions and install latest versions if necessary
if (-not $latestADKInstalled) { if (-not $latestADKInstalled) {
Uninstall-ADK -ADKOption "Windows ADK" -UninstallRegKeys $uninstallRegKeys Uninstall-ADK -ADKOption "Windows ADK"
Install-ADK -ADKOption "Windows ADK" Install-ADK -ADKOption "Windows ADK"
$uninstallKeysUpdated = $true
} }
if (-not $latestWinPEInstalled) { if (-not $latestWinPEInstalled) {
Uninstall-ADK -ADKOption "WinPE add-on" -UninstallRegKeys $uninstallRegKeys Uninstall-ADK -ADKOption "WinPE add-on"
Install-ADK -ADKOption "WinPE add-on" Install-ADK -ADKOption "WinPE add-on"
$uninstallKeysUpdated = $true
} }
if ($uninstallKeysUpdated) { # Define registry path
# Get updated uninstall registry keys $adkPathKey = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows Kits\Installed Roots"
$uninstallRegKeys = Get-ChildItem -Path $uninstallRegPath -Recurse $adkPathName = "KitsRoot10"
}
# Check if ADK installation path exists in registry # Check if ADK installation path exists in registry
$adkRegValueExists = (Get-ItemProperty -Path $adkRegKey -Name $adkRegValueName -ErrorAction SilentlyContinue) $adkPathNameExists = (Get-ItemProperty -Path $adkPathKey -Name $adkPathName -ErrorAction SilentlyContinue)
if ($adkRegValueExists) { if ($adkPathNameExists) {
# Get the ADK installation path # Get the ADK installation path
WriteLog 'Get ADK Path' WriteLog 'Get ADK Path'
$adkPath = (Get-ItemProperty -Path $adkRegKey -Name $adkRegValueName).$adkRegValueName $adkPath = (Get-ItemProperty -Path $adkPathKey -Name $adkPathName).$adkPathName
WriteLog "ADK located at $adkPath" WriteLog "ADK located at $adkPath"
} }
else { else {
@@ -691,30 +669,22 @@ function Get-ADK {
} }
# If ADK was already installed, then check if the Windows Deployment Tools feature is also installed # If ADK was already installed, then check if the Windows Deployment Tools feature is also installed
$deploymentToolsInstalled = Find-InstalledProgramInfo ` $deploymentToolsRegKey = Get-InstalledProgramRegKey -DisplayName "Windows Deployment Tools"
-UninstallRegKeys $uninstallRegKeys `
-RegValueNameFilter "DisplayName" `
-RegValueDataFilter "Windows Deployment Tools" `
-RegValueDataRetrieve "DisplayVersion"
if (-not $deploymentToolsInstalled) { if (-not $deploymentToolsRegKey) {
WriteLog "ADK is installed, but the Windows Deployment Tools feature is not installed." WriteLog "ADK is installed, but the Windows Deployment Tools feature is not installed."
$adkBundleCachePath = Find-InstalledProgramInfo ` $adkRegKey = Get-InstalledProgramRegKey -DisplayName "Windows Assessment and Deployment Kit"
-UninstallRegKeys $uninstallRegKeys ` $adkBundleCachePath = $adkRegKey.GetValue("BundleCachePath")
-RegValueNameFilter "DisplayName" `
-RegValueDataFilter "Windows Assessment and Deployment Kit" `
-RegValueDataRetrieve "BundleCachePath"
if ($adkBundleCachePath) { if ($adkBundleCachePath) {
WriteLog "Installing Windows Deployment Tools..." WriteLog "Installing Windows Deployment Tools..."
$installPath = $adkPath.TrimEnd('\') $adkInstallPath = $adkPath.TrimEnd('\')
Invoke-Process $adkBundleCachePath "/quiet /installpath ""$installPath"" /features OptionId.DeploymentTools" Invoke-Process $adkBundleCachePath "/quiet /installpath ""$adkInstallPath"" /features OptionId.DeploymentTools"
WriteLog "Windows Deployment Tools installed successfully." WriteLog "Windows Deployment Tools installed successfully."
} }
else { else {
throw "Failed to retrieve path to adksetup.exe to install the Windows Deployment Tools. Please manually install it." throw "Failed to retrieve path to adksetup.exe to install the Windows Deployment Tools. Please manually install it."
} }
} }
return $adkPath return $adkPath
} }
function Get-WindowsESD { function Get-WindowsESD {