将网络观察程序扩展更新到最新版本Update the Network Watcher extension to the latest version

概述Overview

Azure 网络观察程序是一项网络性能监视、诊断和分析服务,可对 Azure 网络进行监视。Azure Network Watcher is a network performance monitoring, diagnostic, and analytics service that monitors Azure networks. 若要按需捕获网络流量和使用 Azure VM 上的其他高级功能,必须具备网络观察程序代理虚拟机 (VM) 扩展。The Network Watcher Agent virtual machine (VM) extension is a requirement for capturing network traffic on demand and using other advanced functionality on Azure VMs. 网络观察程序扩展用于连接监视器、连接监视器(预览版)、连接疑难解答和数据包捕获等功能。The Network Watcher extension is used by features like Connection Monitor, Connection Monitor (preview), connection troubleshoot, and packet capture.

先决条件Prerequisites

本文假设你已在 VM 中安装了网络观察程序扩展。This article assumes you have the Network Watcher extension installed in your VM.

最新版本Latest version

当前,网络观察程序扩展的最新版本为 1.4.1693.1The latest version of the Network Watcher extension is currently 1.4.1693.1.

使用 PowerShell 脚本更新扩展Update your extension using a PowerShell script

要进行大型部署的客户需要一次更新多个 VM。Customers with large deployments who need to update multiple VMs at once. 要手动更新选定的 VM,请参阅下一节For updating select VMs manually, please see the next section

<#
    .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.1654.1"
)

function NeedsUpdate($version)
{
    if ($version -eq $MinVersion)
    {
        return $false
    }

    $lessThan = $true;
    $versionParts = $version -split '\.';
    $minVersionParts = $MinVersion -split '\.';
    for ($i = 0; $i -lt $versionParts.Length; $i++)
    {
        if ([int]$versionParts[$i] -gt [int]$minVersionParts[$i])
        {
            $lessThan = $false;
            break;
        }
    }

    return $lessThan
}

Connect-AzAccount -Environment AzureChinaCloud

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;
    $isWindows = $vm.OsVersion -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 ($isWindows) { "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 ($isWindows) {"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"
}

手动更新扩展Update your extension manually

若要更新扩展,需要知道扩展版本。To update your extension, you need to know your extension version.

检查扩展版本Check your extension version

可使用 Azure 门户、Azure CLI 或 PowerShell 检查扩展版本。You can check your extension version by using the Azure portal, the Azure CLI, or PowerShell.

使用 Azure 门户Use the Azure portal

  1. 在 Azure 门户中转到 VM 的“扩展”窗格。Go to the Extensions pane of your VM in the Azure portal.
  2. 选择“AzureNetworkWatcher”扩展,查看“详细信息”窗格。Select the AzureNetworkWatcher extension to see the details pane.
  3. 在“版本”字段中找到版本号。Locate the version number in the Version field.  

使用 Azure CLIUse the Azure CLI

在 Azure CLI 提示符下运行以下命令:Run the following command from an Azure CLI prompt:

az vm get-instance-view --resource-group  "SampleRG" --name "Sample-VM"

找到输出中的“AzureNetworkWatcherExtension”,并从输出中的“TypeHandlerVersion”字段识别版本号 。Locate "AzureNetworkWatcherExtension" in the output and identify the version number from the "TypeHandlerVersion" field in the output.  请注意:有关扩展的信息会在 JSON 输出中多次出现。Please note: Information about the extension appears multiple times in the JSON output. 请在“extensions”块下查看,你会看到扩展的完整版本号。Please look under the "extensions" block and you should see the full version number of the extension.

你会该看到如下内容: Azure CLI 屏幕截图

使用 PowerShellUse PowerShell

在 PowerShell 提示符下运行以下命令:Run the following commands from a PowerShell prompt:

Get-AzVM -ResourceGroupName "SampleRG" -Name "Sample-VM" -Status

找到输出中的 Azure 网络观察程序扩展,并从输出中的“TypeHandlerVersion”字段识别版本号。Locate the Azure Network Watcher extension in the output and identify the version number from the "TypeHandlerVersion" field in the output.  

你会该看到如下内容: PowerShell 屏幕截图

更新扩展Update your extension

如果你的版本低于上面提到的最新版本,请使用以下任一选项更新扩展。If your version is below the latest version mentioned above, update your extension by using any of the following options.

选项 1:使用 PowerShellOption 1: Use PowerShell

运行以下命令:Run the following commands:

#Linux command
Set-AzVMExtension -ResourceGroupName "myResourceGroup1" -Location "ChinaNorth" -VMName "myVM1" -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type "NetworkWatcherAgentLinux"

#Windows command
Set-AzVMExtension -ResourceGroupName "myResourceGroup1" -Location "ChinaNorth" -VMName "myVM1" -Name "NetworkWatcherAgentWindows" -Publisher "Microsoft.Azure.NetworkWatcher" -Type "NetworkWatcherAgentWindows" -ForceRerun "True"

如果这不起作用。If that doesn't work. 使用以下步骤删除扩展并重新安装。Remove and install the extension again, using the steps below. 此操作会自动添加最新版本。This will automatically add the latest version.

删除扩展Removing the extension

#Same command for Linux and Windows
Remove-AzVMExtension -ResourceGroupName "SampleRG" -VMName "Sample-VM" -Name "AzureNetworkWatcherExtension"

重新安装扩展Installing the extension again

#Linux command
Set-AzVMExtension -ResourceGroupName "SampleRG" -Location "chinaeast" -VMName "Sample-VM" -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type "NetworkWatcherAgentLinux" -typeHandlerVersion "1.4"

#Windows command
Set-AzVMExtension -ResourceGroupName "SampleRG" -Location "chinaeast" -VMName "Sample-VM" -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type "NetworkWatcherAgentWindows" -typeHandlerVersion "1.4"

选项 2:使用 Azure CLIOption 2: Use the Azure CLI

强制执行升级。Force an upgrade.

#Linux command
az vm extension set --resource-group "myResourceGroup1" --vm-name "myVM1" --name "NetworkWatcherAgentLinux" --publisher "Microsoft.Azure.NetworkWatcher" --force-update

#Windows command
az vm extension set --resource-group "myResourceGroup1" --vm-name "myVM1" --name "NetworkWatcherAgentWindows" --publisher "Microsoft.Azure.NetworkWatcher" --force-update

如果不起作用,请删除再重新安装该扩展,然后按照以下步骤自动添加最新版本。If that doesn't work, remove and install the extension again, and follow these steps to automatically add the latest version.

删除扩展.Remove the extension.

#Same for Linux and Windows
az vm extension delete --resource-group "myResourceGroup1" --vm-name "myVM1" -n "AzureNetworkWatcherExtension"

再次安装该扩展。Install the extension again.

#Linux command
az vm extension set --resource-group "DALANDEMO" --vm-name "Linux-01" --name "NetworkWatcherAgentLinux" --publisher "Microsoft.Azure.NetworkWatcher"

#Windows command
az vm extension set --resource-group "DALANDEMO" --vm-name "Linux-01" --name "NetworkWatcherAgentWindows" --publisher "Microsoft.Azure.NetworkWatcher"

选项 3:重启 VMOption 3: Reboot your VMs

如果已将网络观察程序扩展的自动升级设置为 True,请重启 VM 安装以获取最新扩展。If you have auto-upgrade set to true for the Network Watcher extension, reboot your VM installation to the latest extension.

支持Support

如果在本文的任何位置需要更多帮助,请参阅 LinuxWindows 的网络观察程序扩展文档。If you need more help at any point in this article, see the Network Watcher extension documentation for Linux or Windows. 还可通过 Azure 支持联系 Azure 专家。You can also contact the Azure experts on the Azure support. 或者,提交 Azure 支持事件。Alternatively, file an Azure support incident. 请转到 Azure 支持站点提交请求。Go to the Azure support site and submit your request. 有关使用 Azure 支持的信息,请阅读 Azure 支持常见问题For information about using Azure Support, read the Azure support FAQ.