提问人:Kevin Charpentier 提问时间:11/2/2023 最后编辑:Mathias R. JessenKevin Charpentier 更新时间:11/2/2023 访问量:12
在 gitlab groups/subgroups/project 中报告用户/成员
Report users/members in gitlab groups/subgroups/project
问:
我开始编写此 powershell 代码,以获取组、子组和项目中所有用户的报告。
但是有一个字段没有给我带来信息,这是用户在该项目上的最后一个活动。
为什么会这样?
即使只需要 10 分钟,是否有可能进一步优化代码?
[CmdletBinding()]
param($token)
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$ErrorActionPreference = "Stop"
$instanceUrl = "https://gitlab.com"
if((Get-Host).Version.Major -le 5){
write-error "Este script no funciona con powershell 5 o anterior por problemas de encoding"
exit 1
}
function call-api
{
param($instanceUrl,
$endpoint,
$params,
$token)
$headers=@{"PRIVATE-TOKEN"=$token}
$response=@()
$page = 1
if($params){ $params = "&$params" }
do
{
$countBeforeInvoke = $response.count
$response+=Invoke-RestMethod -Headers $headers "$instanceUrl/api/v4/$($endpoint)?private_token=$token&per_page=100&page=$page$params"
$page++
}while($response.count -eq $countBeforeInvoke + 100 )
$response
}
$projects = call-api -instanceUrl $instanceUrl -endpoint "projects" -params "membership=true" -token $token
$projects = $projects |? -Property path_with_namespace -Like "nameOfGroupGitLab/*"
# --------------------------------------------------------------------#
# Obtener la lista de proyectos
$projects = call-api -instanceUrl $instanceUrl -endpoint "projects" -params "membership=true" -token $token
$projects = $projects | ? -Property path_with_namespace -like "smg-code/*"
# Itera a través de la lista de proyectos y obtiene información de los usuarios asociados
foreach ($project in $projects) {
# Agrega la propiedad "Users" al objeto $project
$project | Add-Member -MemberType NoteProperty -Name "Users" -Value (call-api -instanceUrl $instanceUrl -endpoint "projects/$($project.id)/members" -token $token)
}
# Exporta datos al archivo CSV
$projects | ForEach-Object {
$project = $_
$project.Users | ForEach-Object {
$user = $_
[PSCustomObject]@{
ProjectId = $project.id
ProjectCreated_at = $project.created_at
ProjectName = $project.name
ProjectWebUrl = $project.web_url
UserId = $user.id
Username = $user.username
Name = $user.name
Created_at = $user.created_at
AccessLevel = $user.access_level
LastActivityOn = $user.last_activity_at
CreatedBy = $user.created_by.username
}
}
} | Export-Csv -Delimiter ";" -Encoding utf8 reportUser.csv
我尝试根据 GitLab 文档重命名变量,在 Gitlab 中,如果有人知道我想要的信息,我找不到 PowerShell 脚本的文档。能够扩大发布有关我公司项目的不同报告的可能性
答: 暂无答案
评论