azure-docs icon indicating copy to clipboard operation
azure-docs copied to clipboard

Error in script logic - Update your extension using a PowerShell script

Open AussieDavo opened this issue 2 years ago • 3 comments

The PowerShell Script in the "Update your extension using a PowerShell script" section does not work correctly.

Every VM it finds it reports as being a Linux VM even though the VM OS is Windows. $vm.OsVersion only gives a version number and not an OS Name so it will never match "Windows" I have substituted $vm.OsVersion with $vm.OsName in the script below to get it working correctly.

The Logic that compares the version number is also not reliable and causes VM's with an AzureNetworkWatcherExtension lower than the MinVersion set in the parameters. I have also rewritten the NeedsUpdate function in the script below casting the variables as Version class.

I also switched out the variable $isWindows with $isitWindows as the following error was being reported in VSCode:

Starting from PowerShell 6.0, the Variable 'isWindows' cannot be assigned any more since it is a readonly automatic variable that is built into PowerShell, please use a different name.

Here's my updated script that works reliably:

<#
    .SYNOPSIS
    This script will scan all VMs in the provided subscription and upgrade any out of date AzureNetworkWatcherExtensions

    .DESCRIPTION
    This script should be no-op if AzureNetworkWatcherExtensions are up to date
    Requires Azure PowerShell 4.2 or higher to be installed (e.g. Install-Module AzureRM).

    .EXAMPLE
    .\UpdateVMAgentsInSub.ps1 -SubID F4BC4873-5DAB-491E-B713-1358EF4992F2 -NoUpdate

#>
[CmdletBinding()]
param(
    [Parameter(Mandatory=$true)]
    [string] $SubID,
    [Parameter(Mandatory=$false)]
    [Switch] $NoUpdate = $false,
    [Parameter(Mandatory=$false)]
    [string] $MinVersion = "1.4.2423.1"
)

function NeedsUpdate($version)
{
    if ([Version]$version -lt [Version]$MinVersion){
        $lessThan = $true
    }else{
        $lessThan = $false
    }
    return $lessThan
}

Write-Host "Scanning all VMs in the subscription: $($SubID)"
Select-AzSubscription -SubscriptionId $SubID;
$vms = Get-AzVM;
$foundVMs = $false;
Write-Host "Starting VM search, this may take a while"

foreach ($vmName in $vms)
{
    # Get Detailed VM info
    $vm = Get-AzVM -ResourceGroupName $vmName.ResourceGroupName -Name $vmName.name -Status;
    $isitWindows = $vm.OsName -match "Windows";
    foreach ($extension in $vm.Extensions)
    {
        if ($extension.Name -eq "AzureNetworkWatcherExtension")
        {
            if (NeedsUpdate($extension.TypeHandlerVersion))
            {
                $foundVMs = $true;
                if (-not ($NoUpdate))
                {
                    Write-Host "Found VM that needs to be updated: subscriptions/$($SubID)/resourceGroups/$($vm.ResourceGroupName)/providers/Microsoft.Compute/virtualMachines/$($vm.Name) -> Updating " -NoNewline
                    Remove-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name "AzureNetworkWatcherExtension" -Force
                    Write-Host "... " -NoNewline
                    $type = if ($isitWindows) { "NetworkWatcherAgentWindows" } else { "NetworkWatcherAgentLinux" };
                    Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -Location $vmName.Location -VMName $vm.Name -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type $type -typeHandlerVersion "1.4"
                    Write-Host "Done"
                }
                else
                {
                    Write-Host "Found $(if ($isitWindows) {"Windows"} else {"Linux"}) VM that needs to be updated: subscriptions/$($SubID)/resourceGroups/$($vm.ResourceGroupName)/providers/Microsoft.Compute/virtualMachines/$($vm.Name)"
                }
            }
        }
    }
}

if ($foundVMs)
{
    Write-Host "Finished $(if ($NoUpdate) {"searching"} else {"updating"}) out of date AzureNetworkWatcherExtension on VMs"
}
else
{
    Write-Host "All AzureNetworkWatcherExtensions up to date"
}

Document Details

Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.

AussieDavo avatar Dec 14 '22 03:12 AussieDavo

@AussieDavo

Thanks for your feedback! We will investigate and update as appropriate.

@AussieDavo Thanks for bringing this to our attention. I'm going to assign this to the document author so they can take a look at it accordingly.

AjayBathini-MSFT avatar Dec 14 '22 12:12 AjayBathini-MSFT

@shijaiswal Can you please check and add your comments on this doc update request as applicable.

AjayBathini-MSFT avatar Dec 14 '22 12:12 AjayBathini-MSFT

@AussieDavo Thank you for your feedback! The script was updated since January. I've just updated the extension version in the script to reflect the latest version. The update should be reflected in the public doc in the next few hours. I'll close this GitHub issue for now, but feel free to comment here as necessary. #please-close

halkazwini avatar Jul 12 '23 18:07 halkazwini