You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.1 KiB

  1. <#
  2. .SYNOPSIS
  3. Deploys a template to Azure
  4. .DESCRIPTION
  5. Deploys an Azure Resource Manager template
  6. .PARAMETER subscriptionId
  7. The subscription id where the template will be deployed.
  8. .PARAMETER resourceGroupName
  9. The resource group where the template will be deployed. Can be the name of an existing or a new resource group.
  10. .PARAMETER resourceGroupLocation
  11. Optional, a resource group location. If specified, will try to create a new resource group in this location. If not specified, assumes resource group is existing.
  12. .PARAMETER deploymentName
  13. The deployment name.
  14. .PARAMETER templateFilePath
  15. Optional, path to the template file. Defaults to template.json.
  16. .PARAMETER parametersFilePath
  17. Optional, path to the parameters file. Defaults to parameters.json. If file is not found, will prompt for parameter values based on template.
  18. #>
  19. param(
  20. [Parameter(Mandatory=$True)]
  21. [string]
  22. $subscriptionId,
  23. [Parameter(Mandatory=$True)]
  24. [string]
  25. $resourceGroupName,
  26. [string]
  27. $resourceGroupLocation,
  28. [Parameter(Mandatory=$True)]
  29. [string]
  30. $deploymentName,
  31. [string]
  32. $templateFilePath = "mesh_rp.linux.json",
  33. [string]
  34. $parametersFilePath = "parameters.json"
  35. )
  36. <#
  37. .SYNOPSIS
  38. Registers RPs
  39. #>
  40. Function RegisterRP {
  41. Param(
  42. [string]$ResourceProviderNamespace
  43. )
  44. Write-Host "Registering resource provider '$ResourceProviderNamespace'";
  45. Register-AzureRmResourceProvider -ProviderNamespace $ResourceProviderNamespace;
  46. }
  47. #******************************************************************************
  48. # Script body
  49. # Execution begins here
  50. #******************************************************************************
  51. $ErrorActionPreference = "Stop"
  52. # sign in
  53. Write-Host "Logging in...";
  54. Login-AzureRmAccount;
  55. # select subscription
  56. Write-Host "Selecting subscription '$subscriptionId'";
  57. Select-AzureRmSubscription -SubscriptionID $subscriptionId;
  58. # Register RPs
  59. $resourceProviders = @();
  60. if($resourceProviders.length) {
  61. Write-Host "Registering resource providers"
  62. foreach($resourceProvider in $resourceProviders) {
  63. RegisterRP($resourceProvider);
  64. }
  65. }
  66. #Create or check for existing resource group
  67. $resourceGroup = Get-AzureRmResourceGroup -Name $resourceGroupName -ErrorAction SilentlyContinue
  68. if(!$resourceGroup)
  69. {
  70. Write-Host "Resource group '$resourceGroupName' does not exist. To create a new resource group, please enter a location.";
  71. if(!$resourceGroupLocation) {
  72. $resourceGroupLocation = Read-Host "resourceGroupLocation";
  73. }
  74. Write-Host "Creating resource group '$resourceGroupName' in location '$resourceGroupLocation'";
  75. New-AzureRmResourceGroup -Name $resourceGroupName -Location $resourceGroupLocation
  76. }
  77. else{
  78. Write-Host "Using existing resource group '$resourceGroupName'";
  79. }
  80. # Start the deployment
  81. Write-Host "Starting deployment...";
  82. if(Test-Path $parametersFilePath) {
  83. New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath -TemplateParameterFile $parametersFilePath;
  84. } else {
  85. New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFile $templateFilePath;
  86. }