Home > Enterprise >  Run custom Powershell script on provisioned Azure VM
Run custom Powershell script on provisioned Azure VM

Time:11-02

I provisioned VM with following C# snippet

var ssrsVm = new WindowsVirtualMachine("vmssrs001", new WindowsVirtualMachineArgs
{
    Name = "vmssrs001",
    ResourceGroupName = resourceGroup.Name,
    NetworkInterfaceIds = { nic.Id },
    Size = "Standard_B1ms",
    AdminUsername = ssrsLogin,
    AdminPassword = ssrsPassword,
    SourceImageReference = new WindowsVirtualMachineSourceImageReferenceArgs
    {
        Publisher = "microsoftpowerbi",
        Offer = "ssrs-2016",
        Sku = "dev-rs-only",
        Version = "latest"
    },
    OsDisk = new WindowsVirtualMachineOsDiskArgs
    {
        Name = "vmssrs001disk",
        Caching = "ReadWrite",
        DiskSizeGb = 200,
        StorageAccountType = "Standard_LRS",
    }
});

After VM has been provisioned I would like to run a custom Powershell script on it to add a firewall rule. Now wondering how to do this as a part of the Pulumi app. With Azure looks like I could do this with RunPowerShellScript but couldn't find anything about it in Pulumi docs, maybe there is a better way to handle my case?

UPDATE

Thanks to Ash's comment I was able to find VirtualMachineRunCommandByVirtualMachine which seems should do what I'm looking for, but unfortunately, following code snippet returns error

var virtualMachineRunCommandByVirtualMachine = new VirtualMachineRunCommandByVirtualMachine("vmssrs001-script",
    new VirtualMachineRunCommandByVirtualMachineArgs
    {
        ResourceGroupName = resourceGroup.Name,
        VmName = ssrsVm.Name,
        RunAsUser = ssrsLogin,
        RunAsPassword = ssrsPassword,
        RunCommandName = "enable firewall rule for ssrs",
        Source = new VirtualMachineRunCommandScriptSourceArgs
        {
            Script =
                @"Firewall AllowHttpForSSRS
            {
                Name                  = 'AllowHTTPForSSRS'
                DisplayName           = 'AllowHTTPForSSRS'
                Group                 = 'PT Rule Group'
                Ensure                = 'Present'
                Enabled               = 'True'
                Profile               = 'Public'
                Direction             = 'Inbound'
                LocalPort             = ('80')
                Protocol              = 'TCP'
                Description           = 'Firewall Rule for SSRS HTTP'
            }"
        }
    });

error The property 'runCommands' is not valid because the 'Microsoft .Compute/RunCommandPreview' feature is not enabled for this subscription."

Looks like other people are struggling with the same here.

CodePudding user response:

You can use a Compute Extension to execute a script against a VM with Pulumi.

This article details some of the options if you just completed the procedure via PowerShell.

  • Related