gpt4 book ai didi

api - 在 AzureDevOps API 的 Invoke-RestMethod 上传递凭据以检索用户

转载 作者:行者123 更新时间:2023-12-02 23:53:20 24 4
gpt4 key购买 nike

我正在尝试创建一个 powershell 脚本,该脚本将使用 REST API 从 AzureDevOps 组织检索大量用户。我已加密我的密码并将其放入凭据中,但是我收到如下错误:

Invoke-RestMethod : Cannot process argument transformation on parameter 
'Credential'.
userName
At D:\Others\Retrieve Users.ps1:11 char:80
+ ... stakeholderUrl -Method Get -Credential $webclient.Credentials).identi
...
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-RestMethod],
ParameterBindingArgumentTransformationException
+ FullyQualifiedErrorId :
ParameterArgumentTransformationError,Microsoft.PowerShell.
Commands.InvokeRestMethodCommand

这是我的 Powershell 脚本代码:

$stakeholderUrl = 
"https://vsaex.dev.azure.com/[organizationName]/_apis/userentitlements?api-
version=5.0-preview.2"

$password = Get-Content D:\GetUsers\password.txt | ConvertTo-SecureString -
Key (Get-Content D:\GetUsers\aes.key)
$credential = New-Object
System.Net.NetworkCredential("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b3c0d2dec3dfd6f3d2d1d09dd0dcde" rel="noreferrer noopener nofollow">[email protected]</a>",$password)


$stakeholder = (Invoke-RestMethod -Uri $stakeholderUrl -Method Get -
Credential $credential).identities

$StakeholderUsers = @()

foreach($user in $stakeholder){

$customObject = new-object PSObject -property @{
"Display Name" = $user.displayName
"Email" = $user.mailAddress
"License" = $user.licenseDisplayName
}

$StakeholderUsers += $customObject
}

$StakeholderUsers | Select `
displayName,
mailAddress,
licenseDisplayName

如果您能在这方面帮助我,我将不胜感激。

谢谢!

最佳答案

以这种方式存储凭据:

domain\username:password

$Credentials= Get-Content D:\GetUsers\Credentials.txt

创建 Base64 编码的基本授权 header :

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}" -f $Credentials)))

调用其余部分:

$result = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

关于api - 在 AzureDevOps API 的 Invoke-RestMethod 上传递凭据以检索用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52736645/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com