Latest posts by Anil Erduran (see all)
- Managing Azure availability sets - Wed, Mar 29 2017
- Manage Azure with Chef – Part 2: Generate a cookbook and recipe - Fri, Feb 17 2017
- Manage Azure with Chef – Part 1: Install Chef on Windows - Thu, Feb 16 2017
In my previous blog on Azure Stack, we discussed about installing Microsoft Azure Stack (MAS) TP2 on a nested Hyper-V environment. Thanks to 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 home lab or even on a desktop PC without needing high-end hardware components.
Now that Azure Stack is deployed, we can think about creating a storage account (blob or table), firing up our very first virtual machine, playing with tenants/plans, and maybe publishing some more content to the existing Azure Stack marketplace.
But before taking the next step, we need our environment ready to accept PowerShell connections. We need additional modules and tools to run existing AzureRM commands targeting an Azure Stack local subscription.
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 see available repositories by using the Get-PSRepository command.
I want to 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
You can now list all available commands imported along with this module using Get-Command cmdlet:
Get-Command -Module AzureRM.Azure StackAdmin
Now 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.
Here 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"
Here we go. Now our Azure Stack environment is registered.
Now you 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.
Cool. Now I can use native AzureRM cmdlets to take actions on my local Azure Stack instance.
If you already read my Azure Storage Services article series, we have used different AzureRM cmdlets to list or take actions on Azure storage resources. As Azure Stack is also a copy of public Azure in your datacenter, we should be able to do same.
Let me try to 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
I 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.
You 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 post was a glimpse of installing and configuring PowerShell environment for Azure Stack. Now you are ready to use native AzureRM commands to target your local Azure Stack environment.