Search code examples
powershellartifactorycredentials

how to authenticate Invoke-RestMethod to list artifactory repositories


Trying to get a list of Repositories from an Artifactory Enterprise v6 instance, using PowerShell 5.1 Invoke-RestMethod from Win10 desktop, but cannot see to get it to authenticate.

Seems simple enough, but this

$myCred = Get-Credential notStanley
$lstART = Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Credential $myCred

only returns the items that allow Anonymous access.

If I open a browser and logon to that Artifactory instance, I can then paste the above URI and get the full list of all repositories my account has access to.

Any hints what the $myCred is missing?


Solution

  • I have tried in the past with artifactory and -Credential doesnt really worked for me.

    I tried the API way which is much simpler and easier to use.

    Connecting to Artifactory with API Key

    Read here to find out how you can get the API key for your account on artifactory.

    $header = @{"X-JFrog-Art-Api" = "yourAPIKey"}
    Invoke-RestMethod -URI https://<myserver>/artifactory/api/repositories -Headers $header
    

    Using Basic Auth and -Credential

    If you do want to work with the Get-Credential prompt, make sure you use the username that works in Artifactory. Its not the same as the domain\user. from here

    $login = Get-Credential -Message "Enter Credentials for Artifactory"
    
    #invalid creds.. but its ok. Need to tell invoke-restmethod to use Basic Auth.
    $headers = @{ Authorization = "Basic Zm9vOmJhcg==" }  
    
    # use -Credential to override the credentials.
    $new = Invoke-RestMethod -URI https://<server>/artifactory/api/repositories -Headers $headers -Credential $login