In this guide, we will walk you through the steps installing and configuring PowerShell for Azure Stack and required tools/modules so that we can leverage existing AzureRM cmdlets for further configuration and management tasks. That will also show us the real consistency between Azure and Azure Stack in terms of PowerShell cmdlets.
In our earlier guide on Azure Stack, we had discussed about installing Microsoft Azure Stack (MAS) TP2 on a nested Hyper-V environment. Recent improvements in Hyper-V, nested virtualization is now officially supported in Windows 10 and Windows Server 2016. Having nested virtualization is a great chance to test and experience Azure Stack environment in your lab or even on a Laptop or desktop PC without needing high-end hardware components.
With Windows Management Framework 5.0, now there is a great way to share your PowerShell work with other community members in a pre-defined format so that anyone can easily use these in their organization.
Using new package manager called “PowerShellGet,” you can search public or private repositories to find PowerShell modules you need. By default, there is one repository called PSGallery, which is a public Microsoft supported place. You can find a bunch of PS commands and DSC resources available there.
You can check available repositories by issuing the Get-PSRepository command.
Lets discuss this repository first because some required modules, most importantly popular AzureRM package, are part of this repository. They can be downloaded using the Install-Module command.
Once your Azure Stack installation is completed, you can RDP to your MAS-CON01 server, open PowerShell with administrative rights, and use following command to install AzureRM module from PowerShell Gallery.
Install-Module -Name AzureRM -RequiredVersion 1.2.6 -Scope CurrentUser
We can list all available commands imported along with this module using Get-Command cmdlet:
Get-Command -Module AzureRM.Azure StackAdmin
Since we have required cmdlets related to Azure Resource Manager tasks such as creating a Resource Group or blob containers and importing Marketplace Gallery items into Azure Stack, just like we have been doing on public Azure for a while. But if you try to use any of the AzureRM cmdlets on Azure Stack box, you will encounter an error telling you that you first need to associate your existing PowerShell session with a subscription.
For Azure Stack, our subscription name is “Default Provider Subscription,” but the AzureRM module does not include required cmdlets to create a connection to an Azure Stack endpoint.
For this, we need to download an additional module called Azure Stack-Tools.
Below is a quick way of obtaining this module:
Invoke-WebRequest https://github.com/Azure/Azure Stack-Tools/archive/master.zip -OutFile master.zip
Expand-Archive master.zip -DestinationPath . -Force
These commands will download Azure Stack-Tools from the GitHub repository and then expand it to the current directory.
There are many functions you can use in this module. The most crucial one is to establish a connection to Azure Stack environment. First, navigate to the “Connect” folder under the module directory and import the Azure Stack.Connect module using:
Import-Module .\Azure Stack.Connect.psm1
Then you can use Add-Azure StackAzureRmEnvironment to target your AzureRM cmdlets to your Azure Stack subscription. You need to provide an additional parameter called AadTenant, which should be set to the Azure Active Directory name you have used for the installation script.
Add-Azure StackAzureRmEnvironment -AadTenant "<mydirectory>.onmicrosoft.com"
Now our Azure Stack environment is registered.
We can target AzureRM cmdlets to our one and only Azure Stack instance using the Add-AzureRmAccount cmdlet.
Add-AzureRmAccount -EnvironmentName Azure Stack -TenantId $AadTenant
Let’s try the same command again to create a new resource group.
Now we can use native AzureRM cmdlets to take actions on my local Azure Stack instance.
List all local storage accounts available:
Get-AzureStorageAccount | Format-Table -Property *
Except for the first one, these are default storage accounts created by the installation script.
Let’s try to create a new container for one of our storage accounts and then upload a blob content using AzureRM cmdlets.
The following command will create a storage account context for my storage account:blob1 so that we can use it for further storage-related tasks.
$Context = Set-AzureRmStorageAccount -ResourceGroupName MASRS01 -Name blob1 -Type Standard_LRS
Actual storage context is inside the .context property of the $context variable.
Creating a new container:
$StorageContainerName = "tempfiles"
New-AzureStorageContainer -Name $StorageContainerName -Permission Container -Context $Context.Context
We have created a new container called “tempfiles” and set the permission to “container,” which provides public access. You can see the newly created container on the Azure Stack portal.
We can also leverage the Set-AzureStorageBlobContent command to upload any local files to the blob storage.
Set-AzureStorageBlobContent -Container $StorageContainerName -File "C:\Users\FabricAdmin\Desktop\Output\Contoso.SimpleVMTemplate.1.0.0.azpkg" -Context $Context.Context
I’m simply trying to upload “Contoso.SimpleVMTemplate.1.0.0.azpkg,” which is a marketplace compressed template file.
We can also use Get-AzureStorageBlobContent to list contents for a specific blob container:
Get-AzureStorageBlob -Container $StorageContainerName -Context $Context.Context | Get-AzureStorageBlobContent | Select -Property *
Azure Stack is the copy of Azure in your datacenter. That consistency makes a bunch of different scenarios available. This article was a glimpse of installing and configuring PowerShell environment for Azure Stack.
Now we are ready to use native AzureRM commands to target your local Azure Stack environment.