How to use Custom Script Extensions for windows using Azure PowerShell - AZ CLI and from Terraform

Custom Script Extension for Windows

The Custom Script Extension downloads and executes scripts on Azure virtual machines. This extension is useful for post deployment configuration, software installation, or any other configuration or management tasks. Scripts can be downloaded from Azure storage or GitHub, or provided to the Azure portal at extension run time. The Custom Script Extension integrates with Azure Resource Manager templates, and can be run using the Azure CLI, PowerShell, Azure portal, or the Azure Virtual Machine REST API.
This document details how to use the Custom Script Extension using the Azure PowerShell module, AZ CLI and then call it from Terraform. 


Content of :-

Create a file in local folder with name:
win_initialize_data_disk.ps1

$disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number

    $letters = 70..89 | ForEach-Object { [char]$_ }
    $count = 0
    $labels = "data1","data2"

    foreach ($disk in $disks) {
        $driveLetter = $letters[$count].ToString()
        $disk |
        Initialize-Disk -PartitionStyle MBR -PassThru |
        New-Partition -UseMaximumSize -DriveLetter $driveLetter |
        Format-Volume -FileSystem NTFS -NewFileSystemLabel $labels[$count] -Confirm:$false -Force
$count++
    }

store this file to storage container :-

 Storage name:-
     rakstr1234
 Container name:-
    myterraformstate

upload the file to container, its URI will be.
 Permission:-


Its fileUris :- https://rakstr1234.blob.core.windows.net/myterraformstate/win_initialize_data_disk.ps1






Set-AzVMCustomScriptExtension -ResourceGroupName web-rg -VMName rakdbserver01 -Location "West US 2" -FileUri https://rakstr1234.blob.core.windows.net/myterraformstate/win_initialize_data_disk.ps1 -Run win_initialize_data_disk.ps1 -name diskinitilization123


                                               another way  using

                                                  az vm extension



Extensions are small applications that provide post-deployment configuration and automation tasks on Azure virtual machines. For example, if a virtual machine requires software installation, anti-virus protection, or Docker configuration, a VM extension can be used to complete these tasks. Extensions can be bundled with a new virtual machine deployment or run against any existing system.
Step1:-

 create a JSON File name jj.json

 {
  "fileUris": ["https://rakstr1234.blob.core.windows.net/myterraformstate/win_initialize_data_disk.ps1"],
  "commandToExecute": "powershell.exe ./win_initialize_data_disk.ps1"
}

The Content of win_initialize_data_disk.ps1

$disks = Get-Disk | Where partitionstyle -eq 'raw' | sort number

    $letters = 70..89 | ForEach-Object { [char]$_ }
    $count = 0
    $labels = "data1","data2"

    foreach ($disk in $disks) {
        $driveLetter = $letters[$count].ToString()
        $disk |
        Initialize-Disk -PartitionStyle MBR -PassThru |
        New-Partition -UseMaximumSize -DriveLetter $driveLetter |
        Format-Volume -FileSystem NTFS -NewFileSystemLabel $labels[$count] -Confirm:$false -Force
$count++
    }

store this file to storage container :-

 Storage name:-
    rakstr1234
 Container name:-
    myterraformstate

fileUris :- https://rakstr1234.blob.core.windows.net/myterraformstate/win_initialize_data_disk.ps1

give at least Blob(anonymous read access for blobs only.




call the JSON File

az vm extension set --resource-group web-rg  --vm-name rakdbserver01 --name CustomScriptExtension --publisher Microsoft.Compute --settings f:\myfolder\qq.json --version 1.9


output:-
{
  "autoUpgradeMinorVersion": true,
  "forceUpdateTag": null,
  "id": "/subscriptions/9239f519-8504-4e92-ae6f-c84d53ba3714/resourceGroups/web-rg/providers/Microsoft.Compute/virtualMachines/rakdbserver01/extensions/CustomScriptExtension",
  "instanceView": null,
  "location": "westus2",
  "name": "CustomScriptExtension",
  "protectedSettings": null,
  "provisioningState": "Succeeded",
  "publisher": "Microsoft.Compute",
  "resourceGroup": "web-rg",
  "settings": {
    "commandToExecute": "powershell.exe ./win_initialize_data_disk.ps1",
    "fileUris": [
      "https://rakstr1234.blob.core.windows.net/myterraformstate/win_initialize_data_disk.ps1"
    ]
  },
  "tags": null,
  "type": "Microsoft.Compute/virtualMachines/extensions",
  "typeHandlerVersion": "1.9",
  "virtualMachineExtensionType": "CustomScriptExtension"
}


Using terraform

   below is for who wants to call the same through terraform.


  resource "null_resource" "Azurediskinitialization_5" {
   provisioner "local-exec" {
         command = "az vm extension set --resource-group web-rg  --vm-name rakdbserver01 --name CustomScriptExtension --publisher Microsoft.Compute --settings jj.json --version 1.9"

                 }
  }

script will execute for 2~3 minutes and then you will find disk initialization extension has installed successfully.






Comments

Popular posts from this blog

How to create a Resource group in Azure using Terraform Part -1

Error inspecting states in the "azurerm" backend: storage: service returned error: StatusCode=403, ErrorCode=AuthenticationFailed