Create Azure File Shares at your ARM template using PowerShell
Using Azure Resource Manage template deployment, you can create a Storage account but you cannot create File Shares. Azure File Shares can be created using the Azure Portal, the Azure PowerShell or the Azure Cli.
Mainly, the idea is to run a PowerShell script that will create the File Shares. This script will be invoked inside the ARM Template. In order to use a PowerShell script from a template, the script must be called from a URL. A good way to provide this is using the Git repository. One major thing to consider is the Storage Account key must be provided to the PowerShell script securely, since the PowerShell script is at a public URL.
The PowerShell script will run inside a Virtual Machine and we will use a CustomScriptExtension Extension to provide it. To use this, at the Virtual Machine Resource of the JSON file add a resources section.
The Custom Script Exception is located at the Virtual Machine resource. Lets assume that the last part of the Virtual Machine resource is the “diagnosticsProfile” so after the closure of the “diagnosticsProfile” we can add the “resources”. Inside the “resources” add the “extensions” resource that will add the “CustomScriptExtension”, like below.
The Template Part
This will be the addition at the Virtual Machine resource:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
"diagnosticsProfile": { "bootDiagnostics": { "enabled": true, "storageUri": "[concat(reference(concat('Microsoft.Storage/storageAccounts/', variables('diagnosticStorageAccountName')), '2016-01-01').primaryEndpoints.blob)]" } } }, "resources": [ { "name": "AzureFileShares", "type": "extensions", "location": "[variables('location')]", "apiVersion": "2016-03-30", "dependsOn": [ "[resourceId('Microsoft.Compute/virtualMachines', parameters('VMName'))]", "[variables('AzureFilesStorageId')]" ], "tags": { "displayName": "AzureFileShares" }, "properties": { "publisher": "Microsoft.Compute", "type": "CustomScriptExtension", "typeHandlerVersion": "1.4", "autoUpgradeMinorVersion": true, "settings": { "fileUris": [ "https://raw.githubusercontent.com/######/#####/master/azurefiles.ps1" ] }, "protectedSettings": { "commandToExecute": "[concat('powershell -ExecutionPolicy Unrestricted -File ','azurefiles.ps1 -SAName ',parameters('AzureFilesStorageName'),' -SAKey ', listKeys(resourceId(variables('AzureFilesStorageAccountResourceGroup'),'Microsoft.Storage/storageAccounts', parameters('AzureFilesStorageName')), '2015-06-15').key1)]" } } } ] }, |
The extension must be depended from the Virtual Machine that will run the script and the Storage Account that will bu used for the file shares.
At the custom script properties add the public RAW url of the PowerShell script.
Next lets see the Storage Account key and execution part. At the connandToExecute section, we will provide a variable that will pass the Storage Account key & Name inside the script for execution. The variable will get the Storage Account key from the Storage Account using the permissions of the Account running the Template Deployment.
Of course to make the template more flexible I have added a variable for the Resource Group and a parameter for the AzureFilesStorageName, so the template will ask for the Storage Account name at the parameters.
The PowerShell
The PowerShell script is tested at Windows Server 2016 VM. You can find it below:
1 2 3 4 5 6 7 8 9 10 11 |
Param ( [Parameter()] [String]$SAKey, [String]$SAName ) Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force Set-PSRepository -Name PSGallery -InstallationPolicy Trusted Install-Module Azure -Confirm:$False Import-Module Azure $storageContext = New-AzureStorageContext -StorageAccountName $SAName -StorageAccountKey $SourceSAKey $storageContext | New-AzureStorageShare -Name ##### |
The post Create Azure File Shares using ARM template & PowerShell appeared first on Apostolidis IT Corner.
Source: e-apostolidis.gr
Add multiple managed disks to Azure RM VM
In this post I have created a PowerShell script to help add multiple managed disks to an Azure RM Virtual Machine.
The script to add multiple managed disks will prompt you to login to an Azure RM account, then it will query the subscriptions and ask you to select the desired. After that it will query the available VMs and promt to select the target VM from the VM list.
At this point I am checking the OS disk and define the storage type of the data disk. If we need to change the storage type we can check the comments at step 4. e.g. If the OS disk is Premium and you want Standard data disks.
The next step is to ask for disk size. You can check the sizes and billing here: https://docs.microsoft.com/en-us/azure/virtual-machines/windows/managed-disks-overview#pricing-and-billing
Finally it will ask for the number of the disk we need to create. After this input the script will create the disks, attach them to the VM and update it.
The Script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# 1. You need to login to the Azure Rm Account Login-AzureRmAccount # 2. The script will query the Subscriptions that the login account has access and will promt the user to select the target Subscription from the drop down list $subscription = Get-AzureRmSubscription | Out-GridView -Title "Select a Subscription" -PassThru Select-AzureRmSubscription -SubscriptionId $subscription.Id # 3. The script will query the available VMs and promt to select the target VM from the VM list $vm = Get-AzureRmVM | Out-GridView -Title "Select the Virtual Machine to add Data Disks to" -PassThru # 4. I set the storage type based on the OS disk. If you want to spesify somehting else you can cahnge this to: $storageType = StandardLRS or PremiumLRS etc. $storageType = $VM.StorageProfile.OsDisk.ManagedDisk.StorageAccountType # 5. The script will promt for disk size, in GB $diskSizeinGB = Read-Host "Enter Size for each Data Disk in GB" $diskConfig = New-AzureRmDiskConfig -AccountType $storageType -Location $vm.Location -CreateOption Empty -DiskSizeGB $diskSizeinGB # 6. Enter how many data disks you need to create $diskquantity = Read-Host "How many disks you need to create?" for($i = 1; $i -le $diskquantity; $i++) { $diskName = $vm.Name + "-DataDisk-" + $i.ToString() $DataDisk = New-AzureRmDisk -DiskName $diskName -Disk $diskConfig -ResourceGroupName $vm.ResourceGroupName $lun = $i - 1 Add-AzureRmVMDataDisk -VM $vm -Name $DiskName -CreateOption Attach -ManagedDiskId $DataDisk.Id -Lun $lun } Update-AzureRmVM -VM $vm -ResourceGroupName $vm.ResourceGroupName |
The post Add multiple managed disks to Azure RM VM appeared first on Apostolidis IT Corner.
Source: e-apostolidis.gr