Skip to content

Commit 7a3532b

Browse files
committed
Merge branch 'main' of https://github.com/MicrosoftDocs/azure-docs-pr into privatelink-disable-policy
2 parents 5573154 + 49a7e4d commit 7a3532b

File tree

1 file changed

+71
-87
lines changed

1 file changed

+71
-87
lines changed

articles/virtual-machines/extensions/network-watcher-update.md

Lines changed: 71 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: shijaiswal
66
tags: azure-resource-manager
77
ms.service: virtual-machines
88
ms.topic: article
9-
ms.date: 11/28/2022
9+
ms.date: 01/16/2023
1010
ms.author: v-ksreedevan
1111
ms.custom: devx-track-azurepowershell
1212

@@ -47,94 +47,78 @@ az vm extension image list-versions --publisher Microsoft.Azure.NetworkWatcher -
4747
Customers with large deployments who need to update multiple VMs at once. For updating select VMs manually, see the next section.
4848

4949
```powershell
50-
<#
51-
.SYNOPSIS
52-
This script will scan all VMs in the provided subscription and upgrade any out of date AzureNetworkWatcherExtensions
53-
54-
.DESCRIPTION
55-
This script should be no-op if AzureNetworkWatcherExtensions are up to date
56-
Requires Azure PowerShell 4.2 or higher to be installed (e.g. Install-Module AzureRM).
57-
58-
.EXAMPLE
59-
.\UpdateVMAgentsInSub.ps1 -SubID F4BC4873-5DAB-491E-B713-1358EF4992F2 -NoUpdate
60-
50+
<#
51+
.SYNOPSIS
52+
This script will scan all VMs in the provided subscription and upgrade any out of date AzureNetworkWatcherExtensions
53+
.DESCRIPTION
54+
This script should be no-op if AzureNetworkWatcherExtensions are up to date
55+
Requires Azure PowerShell 4.2 or higher to be installed (e.g. Install-Module AzureRM).
56+
.EXAMPLE
57+
.\UpdateVMAgentsInSub.ps1 -SubID F4BC4873-5DAB-491E-B713-1358EF4992F2 -NoUpdate
6158
#>
62-
[CmdletBinding()]
63-
param(
64-
[Parameter(Mandatory=$true)]
65-
[string] $SubID,
66-
[Parameter(Mandatory=$false)]
67-
[Switch] $NoUpdate = $false,
68-
[Parameter(Mandatory=$false)]
69-
[string] $MinVersion = "1.4.1974.1"
70-
)
71-
72-
73-
function NeedsUpdate($version)
74-
{
75-
if ($version -eq $MinVersion)
76-
{
77-
return $false
78-
}
79-
80-
$lessThan = $true;
81-
$versionParts = $version -split '\.';
82-
$minVersionParts = $MinVersion -split '\.';
83-
for ($i = 0; $i -lt $versionParts.Length; $i++)
84-
{
85-
if ([int]$versionParts[$i] -gt [int]$minVersionParts[$i])
86-
{
87-
$lessThan = $false;
88-
break;
89-
}
90-
}
91-
92-
return $lessThan
93-
}
94-
95-
Write-Host "Scanning all VMs in the subscription: $($SubID)"
96-
Select-AzSubscription -SubscriptionId $SubID;
97-
$vms = Get-AzVM;
98-
$foundVMs = $false;
99-
Write-Host "Starting VM search, this may take a while"
100-
101-
foreach ($vmName in $vms)
102-
{
103-
# Get Detailed VM info
104-
$vm = Get-AzVM -ResourceGroupName $vmName.ResourceGroupName -Name $vmName.name -Status;
105-
$isWindows = $vm.OsVersion -match "Windows";
106-
foreach ($extension in $vm.Extensions)
107-
{
108-
if ($extension.Name -eq "AzureNetworkWatcherExtension")
109-
{
110-
if (NeedsUpdate($extension.TypeHandlerVersion))
111-
{
112-
$foundVMs = $true;
113-
if (-not ($NoUpdate))
114-
{
115-
Write-Host "Found VM that needs to be updated: subscriptions/$($SubID)/resourceGroups/$($vm.ResourceGroupName)/providers/Microsoft.Compute/virtualMachines/$($vm.Name) -> Updating " -NoNewline
116-
Remove-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name "AzureNetworkWatcherExtension" -Force
117-
Write-Host "... " -NoNewline
118-
$type = if ($isWindows) { "NetworkWatcherAgentWindows" } else { "NetworkWatcherAgentLinux" };
119-
Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -Location $vmName.Location -VMName $vm.Name -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type $type -typeHandlerVersion "1.4"
120-
Write-Host "Done"
121-
}
122-
else
123-
{
124-
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)"
125-
}
126-
}
127-
}
128-
}
129-
}
130-
131-
if ($foundVMs)
132-
{
133-
Write-Host "Finished $(if ($NoUpdate) {"searching"} else {"updating"}) out of date AzureNetworkWatcherExtension on VMs"
59+
60+
[CmdletBinding()]
61+
param(
62+
[Parameter(Mandatory=$true)]
63+
[string] $SubID,
64+
[Parameter(Mandatory=$false)]
65+
[Switch] $NoUpdate = $false,
66+
[Parameter(Mandatory=$false)]
67+
[string] $MinVersion = "1.4.2423.1"
68+
)
69+
function NeedsUpdate($version)
70+
{
71+
if ([Version]$version -lt [Version]$MinVersion)
72+
{
73+
$lessThan = $true
74+
}else{
75+
$lessThan = $false
76+
}
77+
return $lessThan
78+
}
79+
Write-Host "Scanning all VMs in the subscription: $($SubID)"
80+
Set-AzContext -SubscriptionId $SubID
81+
$vms = Get-AzVM
82+
$foundVMs = $false
83+
Write-Host "Starting VM search, this may take a while"
84+
foreach ($vmName in $vms)
85+
{
86+
# Get Detailed VM info
87+
$vm = Get-AzVM -ResourceGroupName $vmName.ResourceGroupName -Name $vmName.name -Status
88+
$isitWindows = $vm.OsName -like "*Windows*"
89+
90+
foreach ($extension in $vm.Extensions)
91+
{
92+
if ($extension.Name -eq "AzureNetworkWatcherExtension")
93+
{
94+
if (NeedsUpdate($extension.TypeHandlerVersion))
95+
{
96+
$foundVMs = $true
97+
if (-not ($NoUpdate))
98+
{
99+
Write-Host "Found VM that needs to be updated: subscriptions/$($SubID)/resourceGroups/$($vm.ResourceGroupName)/providers/Microsoft.Compute/virtualMachines/$($vm.Name) -> Updating " -NoNewline
100+
Remove-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -VMName $vm.Name -Name "AzureNetworkWatcherExtension" -Force
101+
Write-Host "... " -NoNewline
102+
$type = if ($isitWindows) { "NetworkWatcherAgentWindows" } else { "NetworkWatcherAgentLinux" }
103+
Set-AzVMExtension -ResourceGroupName $vm.ResourceGroupName -Location $vmName.Location -VMName $vm.Name -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type $type -typeHandlerVersion $MinVersion
104+
Write-Host "Done"
105+
}
106+
else
107+
{
108+
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)"
109+
}
110+
}
111+
}
112+
}
134113
}
135-
else
136-
{
137-
Write-Host "All AzureNetworkWatcherExtensions up to date"
114+
115+
if ($foundVMs)
116+
{
117+
Write-Host "Finished $(if ($NoUpdate) {"searching"} else {"updating"}) out of date AzureNetworkWatcherExtension on VMs"
118+
}
119+
else
120+
{
121+
Write-Host "All AzureNetworkWatcherExtensions up to date"
138122
}
139123
140124
```

0 commit comments

Comments
 (0)