http://aka.ms/navdemodeploy - using PowerShell


It is part of my DNA, that no matter how easy things have become, it can always become easier.

In this post, I have stated, that the easiest way to create a NAV 2017 environment on Azure is using http://aka.ms/navdemodeploy. But what if you want to create the same environment again and again or you need to create an environment in 5 different languages?

Then there is a lot of typing in names, and passwords and a lot of things that can go wrong.

Can't we automate that part as well?

Of course we can, and the answer is PowerShell.

In the new Azure PowerShell CmdLets there are a set of CmdLets for handling Azure Resource Manager deployments. The following code will produce demo environments with 5 different country versions of NAV:

$VerbosePreference = "Continue"
$ErrorActionPreference = "stop"
Import-Module -Name "AzureRM.Resources"

# Login to your Azure Subscription
Login-AzureRmAccount

# Select subscription
$mysubscriptionID = "<my subscription id>"
Set-AzureRmContext -SubscriptionID $mysubscriptionID

# ARM template
$templateUri = "https://raw.githubusercontent.com/NAVDEMO/NAV2017/master/deploydemo.json"

"W1", "DK", "GB", "NA", "DE" | % {
  # Setup parameter array for ARM template
  $country = $_
  $name = "mynav2017$country"
  $Parameters = New-Object -TypeName Hashtable
  $Parameters.Add("vmName", $name)
  $Parameters.Add("adminPassword", (ConvertTo-SecureString -String "<my password>" -AsPlainText -Force))
  $Parameters.Add("country", $country)
  $Parameters.Add("clickonce", "Yes")
  $Parameters.Add("powerBI", "Yes")
  $Parameters.Add("bingMapsKey", "<my BingMaps key>")
  $Parameters.Add("Office365UserName", "<my Office 365 admin email>")
  $Parameters.Add("Office365Password", (ConvertTo-SecureString -String "<my Office 365 admin password>" -AsPlainText -Force))
  $Parameters.Add("Office365CreatePortal", "Yes")

  # GO!
  $location = "West Europe"
  $resourceGroup = New-AzureRmResourceGroup -Name $name -Location $location
  $resourceGroup | Test-AzureRmResourceGroupDeployment -TemplateUri $templateUri -TemplateParameterObject $Parameters
  $resourceGroup | New-AzureRmResourceGroupDeployment -TemplateUri $templateUri -TemplateParameterObject $Parameters -Name $name
}

and the following code will remove all 5 environments again:

"W1", "DK", "GB", "NA", "DE" | % {
  # Setup parameter array for ARM template
  $country = $_
  $name = "mynav2017$country"
  Remove-AzureRmResourceGroup -Name $name -Force
}

It might not be much faster, but it is a whole lot easier.

Enjoy

Freddy Kristiansen
Technical Evangelist


	
Comments (5)

  1. Syahransyah says:

    Hi Freddy...Very interesting discussion glad that I came across such informative post.

  2. Džoka Mitrov says:

    Hi Freddy

    There's a typo in

    $name = "mynav2017$coutry"

    Best Regards
    Džoka

    1. FreddyDK says:

      Will fix that, thanks

  3. mouli says:

    Hi Freddy,

    tried your script to one of my clinet azure machine it gave me fallowing error. I have provided correct Azure account details only.

    It is working in demo environment.

    SubscriptionName : Pay-As-You-Go
    CurrentStorageAccount :

    Set-AzureRmContext : Provided subscription does not exist
    At line:10 char:1
    + Set-AzureRmContext -SubscriptionID $mysubscriptionID
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [Set-AzureRmContext], ArgumentException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand

    1. FreddyDK says:

      You need to specify the subscription ID - not the subscription Name.

Skip to main content