Created
October 22, 2024 10:38
-
-
Save mikaelkrief/ebb3141d92dc022eac6fdc94e15b0f89 to your computer and use it in GitHub Desktop.
Liste des images de Harbor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-HarborProjectsWithRepositoriesAndExportCsv { | |
| param ( | |
| [string]$HarborURL, # URL de votre instance Harbor (par exemple, https://harbor.mondomaine.com) | |
| [string]$Username, # Nom d'utilisateur pour se connecter à Harbor | |
| [string]$Password, # Mot de passe pour se connecter à Harbor | |
| [string]$OutputFile = "HarborProjects.csv", # Chemin du fichier CSV de sortie | |
| [int]$Page = 1, # Numéro de page (par défaut 1) | |
| [int]$PageSize = 10, # Taille de la page (par défaut 10 projets par page) | |
| [string]$Sort = "name", # Option de tri (par exemple, "name" ou "-name" pour tri inverse) | |
| [string]$ProjectName = "" # Filtre par nom de projet (optionnel) | |
| ) | |
| # Encode l'authentification en Base64 | |
| $authHeader = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$Username:$Password")) | |
| # Prépare l'en-tête HTTP avec les informations d'authentification | |
| $headers = @{ | |
| Authorization = "Basic $($authHeader)" | |
| } | |
| # Tableau pour stocker les résultats avant l'export | |
| $results = @() | |
| # Construction de l'URL pour récupérer les projets avec pagination et tri | |
| $projectsApiEndpoint = "$HarborURL/api/v2.0/projects?page=$Page&page_size=$PageSize&sort=$Sort" | |
| # Si un nom de projet est fourni, ajoute-le en tant que filtre | |
| if ($ProjectName -ne "") { | |
| $projectsApiEndpoint += "&name=$ProjectName" | |
| } | |
| try { | |
| # Exécution de la requête GET vers l'API de Harbor pour obtenir les projets | |
| $projectsResponse = Invoke-RestMethod -Uri $projectsApiEndpoint -Method Get -Headers $headers | |
| # Parcours de chaque projet pour vérifier s'il a des dépôts (repositories) | |
| foreach ($project in $projectsResponse) { | |
| # Récupération des dépôts associés au projet | |
| $repositoriesApiEndpoint = "$HarborURL/api/v2.0/projects/$($project.project_id)/repositories" | |
| $repositoriesResponse = Invoke-RestMethod -Uri $repositoriesApiEndpoint -Method Get -Headers $headers | |
| # Si le projet contient au moins un dépôt, on ajoute les informations dans le tableau | |
| if ($repositoriesResponse.Count -gt 0) { | |
| foreach ($repository in $repositoriesResponse) { | |
| # Récupération des artefacts de chaque dépôt | |
| $artifactsApiEndpoint = "$HarborURL/api/v2.0/projects/$($project.project_id)/repositories/$($repository.name)/artifacts" | |
| $artifactsResponse = Invoke-RestMethod -Uri $artifactsApiEndpoint -Method Get -Headers $headers | |
| foreach ($artifact in $artifactsResponse) { | |
| $artifactTag = $artifact.tags[0].name | |
| # Créer un objet avec les détails du projet, du dépôt et de l'artefact | |
| $results += [pscustomobject]@{ | |
| ProjectName = $project.name | |
| ProjectID = $project.project_id | |
| Repository = $repository.name | |
| ArtifactDigest= $artifact.digest | |
| ArtifactType = $artifact.type | |
| ArtifactTag = $artifactTag | |
| } | |
| } | |
| } | |
| } | |
| } | |
| # Exportation des résultats au format CSV | |
| $results | Export-Csv -Path $OutputFile -NoTypeInformation | |
| Write-Host "Export terminé avec succès. Fichier CSV généré à: $OutputFile" | |
| } | |
| catch { | |
| Write-Host "Erreur: $_" | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment