Connect Powershell with Azure Stack

For lot of a tasks in Azure Stack I need PowerShell, so I will set PowerShell connection from Azure Stack host to Azure Stack. I will user AzureStackAdmin account. I have Azure AD connected environment, so this tutorial will be for that Azure Stack scenario.

I will not be going through prerequisites since they should all already be installed on Azure Host. You can check all the details here – https://docs.microsoft.com/en-us/azure-stack/operator/azure-stack-powershell-configure-admin?view=azs-1910

Be sure that you have valid subscription in Azure, especially if you are using Azure Free account. 

Be sure that your Azure Free subscription is not disabled or that you maybe don’t have any subscriptions on your Azure account.

When you try to add resources via PowerShell to your Azure Stack, and without valid subscription, you will probably get error – New-AzureRmResourceGroup : ‘this.Client.SubscriptionId’ cannot be null.

So be sure to have a subscription on your Azure account if you are using Azure AD connected scenario. If your Free Azure account is expired, you can change subscription to pay as you go.

Open elevated PowerShell and paste in following commands:

# Register an Azure Resource Manager environment that targets your Azure Stack Hub instance. Get your Azure Resource Manager endpoint value from your service provider.
    Add-AzureRMEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" `
      -AzureKeyVaultDnsSuffix adminvault.local.azurestack.external `
      -AzureKeyVaultServiceEndpointResourceId https://adminvault.local.azurestack.external

    # Set your tenant name.
    $AuthEndpoint = (Get-AzureRmEnvironment -Name "AzureStackAdmin").ActiveDirectoryAuthority.TrimEnd('/')
    $AADTenantName = "<myDirectoryTenantName>.onmicrosoft.com"
    $TenantId = (invoke-restmethod "$($AuthEndpoint)/$($AADTenantName)/.well-known/openid-configuration").issuer.TrimEnd('/').Split('/')[-1]

    # After signing in to your environment, Azure Stack Hub cmdlets
    # can be easily targeted at your Azure Stack Hub instance.
    Add-AzureRmAccount -EnvironmentName "AzureStackAdmin" -TenantId $TenantId

You will be asked to login to your Azure Portal, and after that, command will unfold.

Next, I will register resource providers with following command

foreach($s in (Get-AzureRmSubscription)) {
        Select-AzureRmSubscription -SubscriptionId $s.SubscriptionId | Out-Null
        Write-Progress $($s.SubscriptionId + " : " + $s.SubscriptionName)
Get-AzureRmResourceProvider -ListAvailable | Register-AzureRmResourceProvider
    }

You should see Locations as local (Azure Stack instance).

Without registering resources, I would get this error:

New-AzureRmResourceGroup : The provided location ‘Local’ is not available for resource group. List of available regions is ‘centralus,eastasia,sou

theastasia,eastus,eastus2,westus,westus2,northcentralus,southcentralus,westcentralus,northeurope,westeurope,japaneast,japanwest,brazilsouth,austra

liasoutheast,australiaeast,westindia,southindia,centralindia,canadacentral,canadaeast,uksouth,ukwest,koreacentral,koreasouth,francecentral,southaf

ricanorth,uaenorth,australiacentral’.

At line:1 char:1

+ New-AzureRmResourceGroup -Name “MyResourceGroup” -Location “Local”

At, last let’s check if all worked by creating resource group on our Azure Stack. As I said, I’m using Azure Stack host and I will execute command from PowerShell on Azure Stack Host.

New-AzureRmResourceGroup -Name "MyResourceGroup" -Location "Local"

In my example I created a bit different name.

Let’s check if we created anything in our Azure Stack Administration interface. Resource is created ?

Disclaimer