title: Invoke-RestMethod : The remote server returned an error: (401) Unauthorized
body: <p>I would like to get list of groups from Azure devops Security blade. I prepared a code. I am a member of Contributors group in Azure Devops, I am using cmd-let Invoke-RestMethod. I am testing this piece of code connected to my Azure account from laptop, not tested this on Azure Automation or Azure pipelines. I am still facing an issue -->  Error message below: 
  Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.</p>
  
  <pre><code>##My Function
  function GetUrl() {
      param(
          [string]$orgUrl, 
          [hashtable]$header, 
          [string]$AreaId
      )
  
  $orgResourceAreasUrl = [string]::Format("{0}/_apis/resourceAreas/{1}?api-preview=5.0-preview.1", $orgUrl, $AreaId)
  
      # Do a GET on this URL (this returns an object with a "locationUrl" field)
      $results = Invoke-RestMethod -Uri $orgResourceAreasUrl -Headers $header
  
      # The "locationUrl" field reflects the correct base URL for RM REST API calls
      if ("null" -eq $results) {
          $areaUrl = $orgUrl
      }
      else {
          $areaUrl = $results.locationUrl
      }
  
      return $areaUrl
  }
  
    $token =[System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
  $header = @{authorization = "Basic $token"}
  
    $orgUrlAD = "https://vsaex.dev.azure.com/OrganizationName"
  $personalToken = "MyPersonalToken"
  
    ##Function execution
    Write-Host "AD tests"
    $coreAreaId = "xxx"
    $tfsBaseUrl = GetUrl -orgUrl $orgUrlAD -header $header -AreaId 
  $coreAreaId
  
    $projectsUrl = "$($tfsBaseUrl)_apis/groupentitlements?api-version=5.0-preview.1"
  
    $projects = Invoke-RestMethod -Uri $projectsUrl -Method Get -ContentType "application/json" -Headers $header
  
    $projects.value | ForEach-Object {
      Write-Host $_.name
  }
  </code></pre>
  
  <p>Invoke-RestMethod : The remote server returned an error: (401) 
  Unauthorized.</p>