如何在powershell中翻译curl命令

问题描述 投票:1回答:1

我有这个命令:

curl -s -L --header "PRIVATE-TOKEN:XXXX" https://myuri/"

我需要转换为PowerShell

我试过这个,但不起作用:

Invoke-RestMethod -Method Get -Headers @{"AUTHORIZATION"="XXXXXX"} -Uri https://myUri

我也试过这个:

PS > $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
PS > $headers.Add("Authorization","XXXXXX")
PS > $headers.Add("Accept","application/json")
PS > $headers.Add("Content-Type","application/json")
PS> $uri = "https://myUri"
PS> $response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get -ContentType "application/json"
PS> $response
PS> 

但是$response是空的。

有帮助吗?

powershell curl
1个回答
2
投票

这是我用来向Teamcity进行身份验证的一个

    function Connect-to-Teamcity ($userName, $password, $tcServer, $uri)
    {
        $auth = $username + ':' + $password

        $Encoded = [System.Text.Encoding]::UTF8.GetBytes($auth)
        $EncodedPassword = [System.Convert]::ToBase64String($Encoded)


        $headers = @{"Authorization"="Basic $($EncodedPassword)"}

        $url = "http://$tcServer/httpAuth/app/rest"

        $result = Invoke-RestMethod -Uri "$url/$uri" -Header $headers -Method Get

        return $result
    }

如果您有多个标题项,这是另一个示例:

    $resourceAppIdURI = "https://graph.windows.net"
    # Login to Azure and get a token valid for accessing the graph API
    $authority = "https://login.windows.net/$adTenant"
    $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority
    $authResult = $authContext.AcquireToken($resourceAppIdURI, $clientId, $redirectUri, "Auto")
    # Add the token to the header of all future calls to the graph API
    $headers = @{"Authorization"=$authResult.CreateAuthorizationHeader();"Content-Type"="application/json"}
$uri = [string]::Format("https://graph.windows.net/{0}/groups/{1}/appRoleAssignments?api-version=1.5", $adTenant, $groupId)
$body = @"
    {
        "id": $appRoleId,
        "principalId": $groupId,
        "principalType": "Group",
        "resourceId": $appObjectId
    }
"@
$result = Invoke-RestMethod -Method "POST" -Uri $uri -Headers $headers -Body $Body
© www.soinside.com 2019 - 2024. All rights reserved.