1. Answers
  2. Running PowerShell Scripts On Azure Resources

Running PowerShell Scripts on Azure Resources

In this solution, we will demonstrate how to run PowerShell scripts on Azure resources using Pulumi with TypeScript. The key services involved in this solution include Azure Virtual Machines, Azure Automation Accounts, and Runbooks. We will create an Azure Virtual Machine, set up an Azure Automation Account, and create a Runbook to execute the PowerShell script on the VM.

Introduction

In this solution, we will demonstrate how to run PowerShell scripts on Azure resources using Pulumi with TypeScript. The key services involved in this solution include Azure Virtual Machines, Azure Automation Accounts, and Runbooks. We will create an Azure Virtual Machine, set up an Azure Automation Account, and create a Runbook to execute the PowerShell script on the VM.

Step-by-Step Explanation

Step 1: Create an Azure Virtual Machine

We will start by creating an Azure Virtual Machine using Pulumi. This VM will be the target on which we will run our PowerShell script.

Step 2: Set Up an Azure Automation Account

Next, we will set up an Azure Automation Account. This account will be used to manage and execute our Runbooks.

Step 3: Create a Runbook

We will create a Runbook within the Azure Automation Account. This Runbook will contain the PowerShell script that we want to execute on the Azure VM.

Finally, we will link the Runbook to the Azure VM and configure it to execute the PowerShell script on the VM.

Key Points

  • Azure Virtual Machines provide the compute resources needed to run applications and scripts.
  • Azure Automation Accounts allow for the automation of tasks and management of resources.
  • Runbooks are used to define the automation workflows and scripts to be executed.
  • Pulumi allows for the infrastructure to be defined and managed using code, making it easier to automate and manage resources.

Conclusion

In this solution, we demonstrated how to run PowerShell scripts on Azure resources using Pulumi with TypeScript. By leveraging Azure Virtual Machines, Azure Automation Accounts, and Runbooks, we can automate the execution of PowerShell scripts on Azure resources. Pulumi makes it easy to define and manage the infrastructure as code, providing a powerful and flexible way to automate tasks and manage resources in Azure.

Full Code Example

import * as pulumi from "@pulumi/pulumi";
import * as azure from "@pulumi/azure-native";

// Create an Azure Resource Group
const resourceGroup = new azure.resources.ResourceGroup("resourceGroup", {
    resourceGroupName: "myResourceGroup",
    location: "WestUS",
});

// Create an Azure Virtual Machine
const vm = new azure.compute.VirtualMachine("myVM", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    vmName: "myVM",
    networkProfile: {
        networkInterfaces: [{
            id: "<networkInterfaceId>",
        }],
    },
    hardwareProfile: {
        vmSize: "Standard_DS1_v2",
    },
    osProfile: {
        computerName: "myVM",
        adminUsername: "adminuser",
        adminPassword: "Password1234!",
    },
    storageProfile: {
        osDisk: {
            createOption: "FromImage",
            name: "myOsDisk",
        },
        imageReference: {
            publisher: "Canonical",
            offer: "UbuntuServer",
            sku: "18.04-LTS",
            version: "latest",
        },
    },
});

// Create an Azure Automation Account
const automationAccount = new azure.automation.AutomationAccount("myAutomationAccount", {
    resourceGroupName: resourceGroup.name,
    location: resourceGroup.location,
    automationAccountName: "myAutomationAccount",
    sku: {
        name: "Basic",
    },
});

// Create an Azure Automation Credential
const credential = new azure.automation.Credential("myCredential", {
    resourceGroupName: resourceGroup.name,
    automationAccountName: automationAccount.name,
    name: "myCredential",
    userName: "adminuser",
    password: "Password1234!",
});

// Create an Azure Automation Runbook
const runbook = new azure.automation.Runbook("myRunbook", {
    resourceGroupName: resourceGroup.name,
    automationAccountName: automationAccount.name,
    name: "myRunbook",
    logVerbose: true,
    logProgress: true,
    runbookType: "PowerShell",
    publishContentLink: {
        uri: "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-automation-runbook-powershell/Runbooks/HelloWorld.ps1",
    },
});

// Create a Run Command to execute the Runbook on the VM
const runCommand = new azure.compute.VirtualMachineRunCommandByVirtualMachine("myRunCommand", {
    resourceGroupName: resourceGroup.name,
    vmName: vm.name,
    location: resourceGroup.location,
    runCommandName: "myRunCommand",
    source: {
        script: "Start-AzAutomationRunbook -AutomationAccountName 'myAutomationAccount' -Name 'myRunbook' -ResourceGroupName 'myResourceGroup' -CredentialName 'myCredential'",
    },
});

export const vmId = vm.id;
export const automationAccountId = automationAccount.id;
export const runbookId = runbook.id;
export const runCommandId = runCommand.id;

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up