Skip to content

Run Powershell Commands on Azure VMs

To run scripts against multiple VMs in Azure you need to use Invoke-AzVMRunCommand:

invoke-AzVMRunCommand `
         -ScriptPath $ScriptPath `
         -VMName $name `
         -ResourceGroupName $rg `
         -CommandId runpowershellscript `
         -ErrorAction SilentlyContinue 

The $scriptpath variable is the script that contains the actual powershell commands to be run.

To make this easier I created this script which contains 2 functions,
one function CheckConnection will check to see if you are logged in to Azure, and what subscription the script will run against.

The other function, RunScript will get VMs based on a Tag I use in my Azure, OS:{win,linux} and then pass in the parameters Invoke-AzVMRunCommand needs.

You will be prompted to run the script against all Windows, Linux, or “All” VMs (All ignores OS Tag).

## prompt for OS type to filter $os = read-host "Filter on OS? Type All, Win or Linux"

switch ($os)
    {
    All {$script:vms = Get-AzVM -status|Where-object {$_.Powerstate -eq "VM Running"}}
    Win {$script:vms = Get-AzVM -status| Where-Object {$_.StorageProfile.OSDisk.OSType -eq "Windows" -and $_.Powerstate -eq "VM Running"}}
    Linux {$script:vms = Get-AzVM -status| Where-Object {$_.StorageProfile.OSDisk.OSType -eq "Linux"-and $_.Powerstate -eq "VM Running"}}
    }

The script will gather a list of VMs that have the OS tag you selected, and then run a script that is also on your local machine. Look for the lines:

#run the script against the vm, change $ScriptPath to equal the script you want to run
        $ScriptPath = "C:\scripts\win-updates.ps1"

You will need a script with that name in that location. Whatever is in that script will be what is actually run on the VMs.


## Script to run commands against azure vms

# check if you can connect to azure, and what Subscription you are in
function CheckConnection
{
    
$AzStatus = Get-AzContext
if (!$AzStatus)
    {
    Write-Output "Not connected, please run connect-AzAccount"
    }
#if connected to Azure, show what subscription
    else
    {
    write-output "Subscription: "$AzStatus.Subscription.name
    write-output "If this is the wrong subscription use set-azcontext to change subscriptions."
    pause
    }
}

# Function to get azure vms 
function RunScript
{
## prompt for OS type to filter
$os = read-host "Filter on OS? Type All, Win or Linux"


switch ($os)
    {
    All {$script:vms = Get-AzVM -status|Where-object {$_.Powerstate -eq "VM Running"}}
    Win {$script:vms = Get-AzVM -status| Where-Object {$_.StorageProfile.OSDisk.OSType -eq "Windows" -and $_.Powerstate -eq "VM Running"}}
    Linux {$script:vms = Get-AzVM -status| Where-Object {$_.StorageProfile.OSDisk.OSType -eq "Linux"-and $_.Powerstate -eq "VM Running"}}
    }



# filter down by tag
$ENVTag = read-host "Choose Environment, Dev Prod QA UAT Test"

write-output "This will take a few minutes...."

foreach ($vm in $vms)
    {
    
    #get the vms current tags
    #$tags = (Get-AzResource -ResourceGroupName * -Name $vm -ResourceType "Microsoft.Compute/VirtualMachines" ).Tags
    $Tag = $vm.tags
    Write-host "Checking $vm.name for $ENVTAG"
    
        if ($Tag.Environment -contains $ENVtag) 
        {
        $name = ($vm).Name
        $rg = ($vm).ResourceGroupName
        write-host "$name - has tag $ENVtag and is in $rg"

        #run the script against the vm, change $ScriptPath to equal the script you want to run
        $ScriptPath = "C:\scripts\win-updates.ps1"
        invoke-AzVMRunCommand `
         -ScriptPath $ScriptPath `
         -VMName $name `
         -ResourceGroupName $rg `
         -CommandId runpowershellscript `
         -ErrorAction SilentlyContinue 
       
        #write-host "Script run on: $name"
        } 
}
} # End of Rinscript Function

#  RUN: function to check if you are connected to Azure
#CheckConnection

# RUN: function to generate List Of VMS and run the script
RunScript

# RUN: 

Published inAzurePowershell

Be First to Comment

Leave a Reply

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