ps/Modules/Alkami.PowerShell.Choco/Public/Get-ChocoState.ps1

115 lines
3.7 KiB
PowerShell
Raw Normal View History

2023-05-30 22:51:22 -07:00
function Get-ChocoState {
<#
.SYNOPSIS
Returns a list of chocolatey package objects by directly calling choco.exe console application
.PARAMETER LocalOnly
Limit to listing Locally installed packages ONLY
.PARAMETER Source
The chocolatey source to query
.PARAMETER PackageName
The package name to search for
IMPORTANT - By default, chocolatey will search all fields of a package for this string. It is not intrinsically a package "name".
If you specifically want to search for package names, pass the Exact switch.
.PARAMETER All
Pass the "-a" flag to "choco.exe list"
.PARAMETER Pre
Include pre-release packages in results
.PARAMETER GetServiceInfo
Include partial Win32_Service fields in each result
.PARAMETER Exact
ONLY search package NAMES for the string passed as "PackageName"
#>
[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingInvokeExpression', '', Justification = 'Alkami generates this string manually, no user injection')]
[CmdletBinding()]
Param(
[Parameter(Mandatory = $false)]
[Alias("local", "l", "lo")]
[switch]$LocalOnly,
[Parameter(Mandatory = $false)]
[Alias("s")]
[string]$Source = "",
[Parameter(Mandatory = $false)]
[string]$PackageName = "",
[Parameter(Mandatory = $false)]
[string]$PackageVersion = "",
[Parameter(Mandatory = $false)]
[Alias("a")]
[switch]$All,
[Parameter(Mandatory = $false)]
[switch]$Pre,
[Parameter(Mandatory = $false)]
[switch]$GetServiceInfo,
[Parameter(Mandatory = $false)]
[switch]$Exact
)
$logLead = (Get-logLeadName)
$chocoPackages = @();
$localFlag = if ($LocalOnly.IsPresent) { "-l" } else { "" }
$sourceArg = if (![string]::IsNullOrWhitespace($Source)) { "-s ""$Source""" } else { "" }
if (Test-StringIsNullOrWhitespace -Value $PackageName) {
$PackageName = ""
}
if (Test-StringIsNullOrWhitespace -Value $PackageVersion) {
$packageVersionString = ""
} else {
$packageVersionString = "--version $PackageVersion"
}
$allFlag = "";
if ($All.IsPresent) {
$allFlag = "-a -pre"
} elseif ($Pre.IsPresent) {
$allFlag = "-pre"
}
$exactFlag = if ($Exact.IsPresent) { "-e" } else { "" }
$command = "choco list $PackageName $packageVersionString -r $localFlag $sourceArg $allFlag $exactFlag"
$iexCommand = {Invoke-Expression -Command $command}
Write-Host "$logLead : Command: $command"
$chocoList = Invoke-CommandWithRetry -ScriptBLock $iexCommand -Exponential
$chocoList = $chocoList | Where-Object { (![string]::IsNullOrWhitespace($_)) -and ($_ -notmatch "Chocolatey v\d") }
# There are multiple possible connection error messages, that come back across multiple lines
# If another message for connection errors is found, add it to the "regexes"
if ($chocoList -match "Unable to connect|Error retrieving packages from source") {
Write-Error -Message "$logLead : Unable to connect to Package Server"
}
$filteredList = $chocoList.Where( { $_ -notmatch "Unable to connect|Error retrieving packages from source" })
$chocoPackages = (Format-ParseChocoPackages $filteredList)
if ($GetServiceInfo) {
$installedServices = Get-CimInstance Win32_Service
foreach ($package in $chocoPackages) {
foreach ($service in $installedServices) {
if ($package.Name -eq $service.Name) {
$package.IsService = $true
$package.StartMode = $service.StartMode
}
}
}
}
return $chocoPackages
}