Run PowerShell Cmdlet On Azure VMs With Terraform

In this blog post, we will show you how to use Terraform to run PowerShell cmdlets during deployment or post-deployment.

Azure VM extensions

Azure VM extensions are software components that can be installed and configured on virtual machines (VMs) running in Microsoft Azure. They provide additional functionality to VMs, such as security, monitoring, and management. VM extensions can be used to customize the VM configuration, install software, or apply updates without having to redeploy the entire VM.

The Azurerm_virtual_machine_extension Terraform module is a powerful tool that enables users to manage extensions for virtual machines within Microsoft Azure Resource Manager (ARM). This module provides a streamlined method for adding, updating, and removing extensions, enhancing VM functionality and enabling users to customize their VMs to suit their specific requirements. In this article, we will discuss the purpose, usage, and configuration of the azurerm_virtual_machine_extension module in depth.

Code

The following Terraform code block will install a DNS Server in a Windows Server VM using the azurerm_virtual_machine_extension extension.

resource "azurerm_virtual_machine_extension" "dnsserver" {
  name                       = "install-iis"
  virtual_machine_id         = azurerm_windows_virtual_machine.winosvm.id
  publisher                  = "Microsoft.Compute"
  type                       = "CustomScriptExtension"
  type_handler_version       = "2.0"
  auto_upgrade_minor_version = true

  settings = <<SETTINGS
    {
      "commandToExecute": "powershell -ExecutionPolicy Unrestricted Install-WindowsFeature -Name DNS -IncludeManagementTools"
 
   }
  SETTINGS
}

CustomScriptExtention

If you notice, we are using the CustomScriptExtention. The Azure CustomScriptExtension is a versatile VM extension that allows users to automate tasks and run custom scripts on Azure Virtual Machines (VMs) during or after deployment. This extension enables users to configure the VM environment, install software packages, and perform other custom actions to suit their specific requirements. In this article, we will discuss the purpose, usage, and configuration of the Azure CustomScriptExtension VM extension.

Processing…
Success! You're on the list.


Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.