title: How to prevent error "WARNING: Unable to acquire token for tenant 'tenantid'" when running Powershell scripts in Azure
body: <p>I'm creating a Service principal to be used as an Azure Runas Account for Azure Automation, using a Powershell script.  The script works, however I get the following warning when it's completed<br>
  WARNING: Unable to acquire token for tenant 'tenantID'.  </p>
  
  <p>The tenantID from the warning message is another tenant that my account has access to, which has multiple subscriptions within it.  However it's unrelated to the tenantid and subscription I'm logging in to. </p>
  
  <p>I've tried logging in via the Powershell window, then running the script without having the login inside the script, but get the same error. When I run get-AzContext in the Powershell window after the script runs, it lists the correct tenantID </p>
  
  <p>Function being used to login is below.  the tenant ID is not the same as the one I get the Warning for</p>
  
  <pre><code>function Login {
      # Log in
      $tenantid = "tenantID"
      $subscriptionId = "subscriptionID"
      $subscriptionName = "subscriptionname"
      Clear-AzContext -Force
      Message("Logging In")
      $account = $(Get-AzContext).Account
      if ([string]::IsNullOrEmpty($account)) {
          Login-AzAccount -Tenant $tenantid -Subscription $subscriptionId
      }
      # Select the subscription
  
      Message("Selecting the '$subscriptionName' Subscription")
      Set-AzContext $subscriptionId | Out-Null
  }
  
  
  I have no other references to tenantID.  The only other reference I have is for the subscriptionID, in a script which is called by the original script.
  $Subscription = $(Get-AzContext).Subscription
  
  I'd like to understand why it's trying to access the different TenantID for a token, and not to have the error when running the script
  </code></pre>