Files
marco.locatelli@steamware.net 23137a0fed refactor contatori + 1 -> ++
2026-05-13 15:17:07 +02:00

85 lines
2.8 KiB
PowerShell

<#
.SYNOPSIS
Recupera utenti GitLab per ID 1-100 e scrive output in C:\Steamware\Logs\Gitlab\gitlab_users_1-100.log
#>
#importo file contenente configurazioni
. .\ApiGit\ApiGitResources\ApiScriptsConfig.ps1
#importo file contenente funzioni
. .\ApiGit\ApiGitResources\ApiScriptsFunctions.ps1
$GitlabUrl = "gitlab.steamware.net"
$PrivateToken = $tokenGitlab
# Configurazione
$StartId = 1
$EndId = 50
$LogDir = "C:\Steamware\Logs\Gitlab"
$LogFile = Join-Path -Path $LogDir -ChildPath "GitlabUsers.log"
# Crea cartella log se non esiste
try {
if (-not (Test-Path -Path $LogDir)) {
New-Item -Path $LogDir -ItemType Directory -Force | Out-Null
}
}
catch {
Write-Error "Impossibile creare la cartella di log $LogDir. Errore: $($_.Exception.Message)"
exit 1
}
# Header autenticazione
$baseUrl = $GitlabUrl.TrimEnd('/')
$headers = @{ "PRIVATE-TOKEN" = $PrivateToken }
# Inizializza file di log (sovrascrive se esiste)
$startLine = "INFO [{0}] Starting GitLab users retrieval for IDs {1}-{2}" -f (Get-Date -Format o), $StartId, $EndId
$startLine | Out-File -FilePath $LogFile -Encoding UTF8
$retrieved = 0
$errors = 0
# inizializza contatore per numeratore
$counter = 1
$total = $EndId - $StartId + 1
for ($id = $StartId; $id -le $EndId; $id++) {
$url = "$baseUrl/api/v4/users/$id"
# crea prefisso numerato e separatore (es. --- 001/100 ---)
$num = "{0:D3}" -f $counter
$separator = "----- $num/$total -----"
try {
$user = Invoke-RestMethod -Uri $url -Headers $headers -Method Get -ErrorAction Stop
$json = $user | ConvertTo-Json -Depth 10 -Compress
$line = "{0} `nINFO [{1}] USER_FOUND id={2} data={3}" -f $separator, (Get-Date -Format o), $id, $json
$line | Out-File -FilePath $LogFile -Encoding UTF8 -Append
$retrieved++
}
catch {
$status = "unknown"
try {
if ($_.Exception.Response -ne $null) {
$status = $_.Exception.Response.StatusCode.value__
}
} catch { $status = "unknown" }
if ($status -eq 404) {
$line = "{0} `nWARN [{1}] USER_NOT_FOUND id={2} status=404 message=Not Found" -f $separator, (Get-Date -Format o), $id
}
else {
$msg = $_.Exception.Message -replace "`r?`n", " "
$line = "{0} `nERROR [{1}] USER_ERROR id={2} status={3} message={4}" -f $separator, (Get-Date -Format o), $id, $status, $msg
}
$line | Out-File -FilePath $LogFile -Encoding UTF8 -Append
$errors++
}
$counter++
Start-Sleep -Milliseconds 200
}
$endLine = "INFO [{0}] Completed. Retrieved={1} Errors={2}" -f (Get-Date -Format o), $retrieved, $errors
$endLine | Out-File -FilePath $LogFile -Encoding UTF8 -Append
Write-Host "Fatto. Log salvato in $LogFile. Utenti recuperati: $retrieved. Errori: $errors."