ps/Modules/Alkami.DevOps.Inventory/Public/Get-ChocolateyInventory.ps1
2023-05-30 22:51:22 -07:00

102 lines
4.1 KiB
PowerShell

function Get-ChocolateyInventory {
<#
.SYNOPSIS
Get a PSObject containing categorized packages which represents the assumed list of what is installed on a machine, per BoRG
.EXAMPLE
Get-ChocolateyInventory
.PARAMETER WithTags
Switch indicating whether or not to include package tags
#>
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[switch]$WithTags
)
$logLead = (Get-LogLeadName);
$providerStopWatch = [System.Diagnostics.StopWatch]::StartNew()
$chocoDictionary = New-Object System.Collections.Specialized.OrderedDictionary
$chocoDetails = New-Object System.Collections.Specialized.OrderedDictionary
$chocoPackages = New-Object System.Collections.Specialized.OrderedDictionary
$chocoSettings = New-Object System.Collections.Specialized.OrderedDictionary
$chocoSources = New-Object System.Collections.Specialized.OrderedDictionary
$requiredChocoPackages = New-Object System.Collections.Specialized.OrderedDictionary
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Reading Choco Sources"
$sources = Get-ChocolateySources
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Done Reading Choco Sources"
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Reading Choco Packages"
$localChocos = Get-ChocoState -l -getServiceInfo
Set-ChocoPackageSourceFeeds $localChocos
if ($WithTags.IsPresent) {
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Retrieving tags"
Set-ChocoPackageTags $localChocos
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Done retrieving tags"
}
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Done Reading Choco Packages"
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Reading Miscellaneous Choco Info"
$chocoVersion = choco --version -r
$chocoFeatures = choco features -r
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Done Reading Miscellaneous Choco Info"
# Handle Version
$chocoDictionary["ChocolateyVersion"] = $chocoVersion
# Handle Features
foreach ($feature in $chocoFeatures) {
$featureSplit = $feature.Split("|")
$chocoSettings.Add($featureSplit[0], $featureSplit[1])
}
# Handle Sources
foreach ($source in $sources) {
$chocoSources.Add($source.Name, ($source | Select-Object Source, IsDefault, Priority, Disabled, IsSdk))
}
$chocoInstallPath = Get-ChocolateyInstallPath
$chocoFolder = Join-Path $chocoInstallPath "lib"
# Handle Packages
foreach ($package in $localChocos) {
# I dislike putting this here, but I don't think it's worth extracting. Maybe.
$packageFolder = Join-Path $chocoFolder "$($package.Name)"
$nuspecPath = Join-Path $packageFolder "$($package.Name).nuspec"
[xml]$nuspecContent = Read-XMLFile -xmlPath $nuspecPath
$nuspecVersion = $nuspecContent.Package.Metadata.Version
$packageResult = @{
Name = $package.Name
IsSDK = $package.Feed.IsSDK;
IsService = $package.IsService;
SourceName = $package.Feed.Name;
SourceUrl = $package.Feed.Source;
StartMode = $package.StartMode;
Tags = $package.Tags;
Version = $package.Version;
NuSpecVersion = $nuspecVersion;
}
$chocoPackages.Add($package.Name, (New-Object PSObject -Property $packageResult))
}
# This property is legacy from SRE-13518 and can probably go if you're doing future work
# We left it in case someone does $x.requiredChocoPackages.Name or somesuch, so it's not _$null_
$chocoDetails.Add("requiredChocoPackages", $requiredChocoPackages)
$chocoDetails.Add("Packages", $chocoPackages)
$chocoDetails.Add("Settings", $chocoSettings)
$chocoDetails.Add("Sources", $chocoSources)
$chocoDictionary.Add("Chocolatey", $chocoDetails)
Write-Verbose "$logLead : [$($providerStopWatch.Elapsed)] : Provider Complete"
$providerStopWatch.Stop()
return $chocoDictionary
}