function Remove-OrphanedChocoFolders { <# .SYNOPSIS Remove folders and files left behind by choco uninstall .DESCRIPTION Accepts either a package name or can search all packages to find choco packages with files left behind .Notes This funciton uses a parameter set for input parameters to determine if the function should crawl all packages or inspect the folder of the package name provided .EXAMPLE This example Shows how to crawl all packages to look for orphaned folders Remove-OrphanedChocoFolders -AllPackages This example Shows how to specify a package name to inspect as an orphaned folder Remove-OrphanedChocoFolders -PackageName "Alkami.ms.package.name" #> [cmdletbinding()] param( [Parameter(Mandatory = $true, ParameterSetName = "ByPackageName")] [string]$PackageName, [Parameter(Mandatory = $true, ParameterSetName = "ByAllPackages")] [switch]$AllPackages ) $logLead = Get-LogLeadName if (($PSCmdlet.ParameterSetName -eq "ByAllPackages") -and ($AllPackages -ne $true)) { Write-Error "$logLead : You supplied -AllPackages, but it was not true, check your calling code" return } Write-Host "$logLead : Searching for orphaned choco folders" $chocoInstallPath = Get-ChocolateyInstallPath $chocoLibDirectory = Join-Path $chocoInstallPath "lib" If ([string]::IsNullOrEmpty($chocoLibDirectory) ) { # While this is a problem, we dont want to halt the deploy pipline for this, rather we will report a build statistic problem Write-Warning "$loglead : choco lib directory to search was null, stopping" Write-Host "##teamcity[buildStatisticValue key='FailedToRemoveOrphanedFolder' value='1']" return } if ($PSCmdlet.ParameterSetName -eq "ByPackageName" ) { if ([string]::IsNullOrEmpty($PackageName) -eq $false) { Write-Host "$logLead : Checking $PackageName" $packageDirectory = Join-Path -Path $chocoLibDirectory -ChildPath $PackageName $packageFolderContents = Get-ChildItem -Path $chocoLibDirectory -Filter $PackageName -ErrorAction SilentlyContinue if ($null -ne $packageFolderContents) { Remove-FileSystemItem -Path $packageDirectory -Force Write-Host "##teamcity[buildStatisticValue key='SuccesfullyRemovedOrphanedFolder' value='1']" return } else { Write-Host "$loglead : No folder found to remove for path $packageDirectory" return } } else { Write-Host "$logLead : PacakgeName was null or empty, but you supplied the PackageName param" Write-Host "##teamcity[buildStatisticValue key='FailedToRemoveOrphanedFolder' value='1']" return } } if (($PSCmdlet.ParameterSetName -eq "ByAllPackages") -and ( $AllPackages -eq $true)) { Write-Host "$logLead : Checking all folders" $chocoLibFolders = Get-ChildItem -Path $chocoLibDirectory -Directory -Exclude "chocolatey" # exclude the chocolatey folder if ($null -eq $chocoLibFolders ) { Write-Host "$logLead : No choco package folders found to search for missing nuspecs" return } foreach ($chocoLibFolder in $chocoLibFolders) { Write-Host "$loglead : Interogating $chocoLibFolder" $nuspecPath = $null $nuspecPath = Get-ChildItem -Path $chocoLibFolder.FullName -Filter "*.nuspec" if ($null -eq $nuspecPath) { Write-Warning "$logLead : Could not find nuspec for $chocoLibFolder" try { Remove-FileSystemItem -Path $chocoLibFolder -Force Write-Host "##teamcity[buildStatisticValue key='SuccesfullyRemovedOrphanedFolder' value='1']" } catch { Write-Warning "$logLead : Unable to remove folder $chocoLibFolder" Write-Warning $_ Write-Host "##teamcity[buildStatisticValue key='FailedToRemoveOrphanedFolder' value='1']" } } else { Write-Host "$logLead : Found nuspec for $chocoLibFolder" continue } } } }