cleanartifactory.ps1
param( # Parameter help description [Parameter(Mandatory)] [String] $Repository="ext-snapshot-local", # Parameter help description [Parameter(Mandatory)] [String] $Path = "/haven/backend", # Parameter help description [Parameter(Mandatory)] [String] $Username = "admin", # Parameter help description [Parameter(Mandatory)] [String] $Password = "veSi6UGu29", # Parameter help description [Parameter()] [Int] $KeepFileCount = 15 ) function Invoke-AFRequest { [CmdletBinding()] param( [Parameter(Mandatory)]c [ValidateNotNullOrEmpty()] [string] $Path, [Parameter()] [ValidateSet('Delete', 'Get', 'Post', 'Put')] [string] $Method = 'Get', [Parameter()] [ValidateNotNullOrEmpty()] [hashtable] $Headers = @{}, [Parameter()] [ValidateNotNullOrEmpty()] [string] $Accept, [Parameter()] [ValidateNotNullOrEmpty()] [HashTable] $Body, [Parameter()] [ValidateNotNullOrEmpty()] [string] $ContentType = 'application/x-www-form-urlencoded', [Parameter()] [ValidateSet("RepoUri","APIUri")] [string] $Uri = "APIUri", [Parameter(Mandatory)] [string] $Username, [Parameter(Mandatory)] [string] $Password ) $FullUri = "https://draxdigital.jfrog.io/draxdigital" $IwrParams = @{ Uri = $FullUri + $Path Method = $Method ContentType = $ContentType } $Credential = New-Object System.Management.Automation.PsCredential($Username,(ConvertTo-SecureString $Password -AsPlainText -Force)) $IwrParams += @{ Credential = $Credential } if($Body){ $IwrParams += @{ Body = $Body} } #$IwrParams | ConvertTo-Json -Depth 4 try{ Invoke-RestMethod @IwrParams } catch { $_.Exception | ConvertTo-Json -Depth 1 } } function Invoke-AFApi { [CmdletBinding()] param( [Parameter(Mandatory)] [ValidateNotNullOrEmpty()] [string] $Path, [Parameter()] [ValidateSet('Delete', 'Get', 'Post', 'Put')] [string] $Method = 'Get', [Parameter()] [ValidateNotNullOrEmpty()] [hashtable] $Headers = @{}, [Parameter()] [ValidateNotNullOrEmpty()] [string] $Accept, [Parameter()] [ValidateNotNullOrEmpty()] [HashTable] $Body, [Parameter()] [ValidateNotNullOrEmpty()] [string] $ContentType = 'application/x-www-form-urlencoded', [Parameter()] [ValidateSet("RepoUri","APIUri")] [string] $Uri = "APIUri", [Parameter(Mandatory)] [string] $Username, [Parameter(Mandatory)] [String] $Password ) $FullUri = "https://draxdigital.jfrog.io/draxdigital/api" $IwrParams = @{ Uri = $FullUri + $Path Method = $Method ContentType = $ContentType } $Credential = New-Object System.Management.Automation.PsCredential($Username,(ConvertTo-SecureString $Password -AsPlainText -Force)) $IwrParams += @{ Credential = $Credential } if($Body){ $IwrParams += @{ Body = $Body} } #$IwrParams | ConvertTo-Json -Depth 4 try{ Invoke-RestMethod @IwrParams } catch { $_.Exception | ConvertTo-Json -Depth 1 } } Function Remove-AFItem { param( # Parameter help description [Parameter(Mandatory)] [String] $Password, # Parameter help description [Parameter(Mandatory)] [String] $Username, # Parameter help description [Parameter(Mandatory)] [String] [alias("Uri")] $FullPath ) if($Repository -eq $null){ throw "Reposiotry null. Configure using Set-AFServer -Repository 'RepositoryName'" } Invoke-AFRequest -Path $FullPath -Method "Delete" -Username $Username -Password $Password } function Get-AFChildItem { param ( # Parameter help description [Parameter()][ValidateNotNullOrEmpty()] [String] $Repository = ($script:AFServer.Repository), # Parameter help description [Parameter()] [Int32] $Depth = 1, # Parameter help description [Parameter()] [switch] $IncludeFolders, # Parameter help description [Parameter()] [String] $Path, # Parameter help description [Parameter()] [String] $Username, # Parameter help description [Parameter()] [String] $Password ) $listFolders = 0 if($IncludeFolders){$listFolders = 1} if($Path){ $Uri = "/storage/$($Repository)$($Path)?list&includeRootPath=0&deep=1&depth=$Depth&mdTimestamps=1&listFolders=$listFolders" }else{ $Uri = "/storage/$($Repository)?list&includeRootPath=0&deep=1&depth=$Depth&mdTimestamps=1&listFolders=$listFolders" } $Results = Invoke-AFApi -Path $Uri -Username $Username -Password $Password $Results.files } #Set-AFCredential -UserId 'admin' -Password 'veSi6UGu29' #$Files = Get-AFChildItem -Username $Username -Password $Password -Repository $Repository -Path $Path #| ? { $PSItem.uri -like "/*-backend-*.zip" -and $PSItem.Uri -notlike "*latest*"} | Sort-Object -Property lastModified $Files = Get-AFChildItem -Username $Username -Password $Password -Repository $Repository -Path $Path | Sort-Object -Property lastModified $FileCount = $files.Count if($FileCount -gt $KeepFileCount){ $KeepFileCount = $FileCount - $KeepFileCount }else{ $KeepFileCount = 0 } ($Files | Select-Object -First $KeepFileCount) | % { $FullPath = "/" + $Repository + $Path + $PSItem.uri Write-Host "Deleting: $FullPath" -ForegroundColor Red Remove-AFItem -FullPath $FullPath -Username $Username -Password $Password } Write-Host "Clearing Trashcan" Invoke-AFApi -Path '/trash/empty' -Method "POST" -Username $Username -Password $Password |