Skip to content

[Az.Network] Updated New-AzNetworkWatcherPacketCaptureV2 command and added a new sub command 'New-AzPacketCaptureSettingsConfig' for Network watcher Packet capture include ring buffer change. #27880

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
Jun 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
6985eb0
Added a new command and related update for continuous capture and its…
v-sapanchal Jun 2, 2025
5b063be
Added help doc, mapped in network command,Test case and session records
v-sapanchal Jun 2, 2025
b7522c4
Merge branch 'main' into spanchal/pcap_ring_buffer_new
v-sapanchal Jun 5, 2025
0ec50e2
Modified the change log as per the new offerings
v-sapanchal Jun 5, 2025
252f3cd
StaticAnalysis issue
v-sapanchal Jun 5, 2025
2d2eccc
reverted to the old file format and added our records for supress
v-sapanchal Jun 6, 2025
2df74d7
Added some Test assert regarding capture settings
v-sapanchal Jun 13, 2025
7afeea3
Resolve PR comments
v-sapanchal Jun 16, 2025
83ca579
Added a test case for VM ring buffer
v-sapanchal Jun 16, 2025
ae9d955
Updated the md file new-aznetworkwatcherpacketcapturev2
v-sapanchal Jun 16, 2025
cafad9a
Add confirmation for default values in true case and updated the defa…
v-sapanchal Jun 18, 2025
ae7418b
resolve the PR comments for changing the default information and remo…
v-sapanchal Jun 19, 2025
34a8a33
Resolved PR comments
v-sapanchal Jun 20, 2025
50d50bf
revert the file as before changes anything
v-sapanchal Jun 20, 2025
4f47bef
revert exampleissue file
v-sapanchal Jun 20, 2025
1a3e3e7
Change for test failing in PS build
v-sapanchal Jun 20, 2025
3d08897
resolve static analysis issue for SupportsShouldProcess
v-sapanchal Jun 20, 2025
8a28d8e
Suppress the command error for ShouldProcess
v-sapanchal Jun 20, 2025
32c6ee3
nit, PR comment
v-sapanchal Jun 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Accounts/Accounts/Utilities/CommandMappings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5158,6 +5158,7 @@
"New-AzP2sVpnGateway": {},
"New-AzPacketCaptureFilterConfig": {},
"New-AzPacketCaptureScopeConfig": {},
"New-AzPacketCaptureSettingsConfig": {},
"New-AzPrivateDnsZoneConfig": {},
"New-AzPrivateDnsZoneGroup": {},
"New-AzPrivateEndpoint": {},
Expand Down
16 changes: 16 additions & 0 deletions src/Network/Network.Test/ScenarioTests/NetworkWatcherAPITests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ public void TestPacketCaptureVMSS()
TestRunner.RunTestScript("Test-PacketCaptureV2");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(Category.Owner, NrpTeamAlias.netanalyticsdev)]
public void TestPacketCaptureVMWithRingBuffer()
{
TestRunner.RunTestScript("Test-PacketCaptureV2ForVMWithRingBuffer");
}

[Fact]
[Trait(Category.AcceptanceType, Category.CheckIn)]
[Trait(Category.Owner, NrpTeamAlias.netanalyticsdev)]
public void TestPacketCaptureVMSSWithRingBuffer()
{
TestRunner.RunTestScript("Test-PacketCaptureV2WithRingBuffer");
}

[Fact]
[Trait(Category.AcceptanceType, Category.LiveOnly)]
[Trait(Category.Owner, NrpTeamAlias.netanalyticsdev)]
Expand Down
169 changes: 169 additions & 0 deletions src/Network/Network.Test/ScenarioTests/NetworkWatcherAPITests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,175 @@ function Test-PacketCaptureV2 {
}
}

<#
.SYNOPSIS
Test PacketCapture API for VM.
#>
function Test-PacketCaptureV2ForVMWithRingBuffer {
# Setup
$resourceGroupName = Get-NrpResourceGroupName
$nwName = Get-NrpResourceName
$location = Get-PilotLocation
$resourceTypeParent = "Microsoft.Network/networkWatchers"
$nwLocation = Get-ProviderLocation $resourceTypeParent
$nwRgName = Get-NrpResourceGroupName
$templateFile = (Resolve-Path ".\TestData\Deployment.json").Path
$pcName1 = Get-NrpResourceName

try {
. ".\AzureRM.Resources.ps1"

# Create Resource group
New-AzResourceGroup -Name $resourceGroupName -Location "$location"

# Deploy resources
Get-TestResourcesDeployment -rgn "$resourceGroupName"

# Create Resource group for Network Watcher
New-AzResourceGroup -Name $nwRgName -Location "$location"

# Get Network Watcher
$nw = Get-CreateTestNetworkWatcher -location $location -nwName $nwName -nwRgName $nwRgName

#Get Vm
$vm = Get-AzVM -ResourceGroupName $resourceGroupName

#Install networkWatcherAgent on Vm
Set-AzVMExtension -ResourceGroupName "$resourceGroupName" -Location "$location" -VMName $vm.Name -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type "NetworkWatcherAgentWindows" -TypeHandlerVersion "1.4"

# Create Capture settiings for packet capture, its only applicable if we are pass continuousCapture as true/false
$c1 = New-AzPacketCaptureSettingsConfig -FileCount 2 -FileSizeInBytes 102400 -SessionTimeLimitInSeconds 60

#Create packet capture
$job = New-AzNetworkWatcherPacketCaptureV2 -NetworkWatcher $nw -Name $pcName1 -TargetId $vm.Id -ContinuousCapture $true -CaptureSetting $c1 -LocalPath C:\captures\Capture.cap -AsJob
$job | Wait-Job

#Get packet capture
$job = Get-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName1 -AsJob
$job | Wait-Job
$pc1 = $job | Receive-Job

Write-Output "pc1 Name: $($pc1.Name)"

#Verification
Assert-AreEqual $pc1.Name $pcName1
Assert-AreEqual "Succeeded" $pc1.ProvisioningState
Assert-AreEqual 2 $c1.FileCount
Assert-AreEqual 102400 $c1.FileSizeInBytes
Assert-AreEqual 60 $c1.SessionTimeLimitInSeconds
Assert-Null $pc1.TotalBytesPerSession
Assert-Null $pc1.TimeLimitInSeconds

#Stop packet capture
$job = Stop-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName1 -AsJob
$job | Wait-Job

#Get packet capture
$pc1 = Get-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName1

#Remove packet capture
$job = Remove-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName1 -AsJob
$job | Wait-Job
}
finally {
# Cleanup
Clean-ResourceGroup $resourceGroupName
#Clean-ResourceGroup $nwRgName
}
}

<#
.SYNOPSIS
Test PacketCapture API with ring buffer.
#>
function Test-PacketCaptureV2WithRingBuffer {
# Setup
$resourceGroupName = Get-NrpResourceGroupName
$virtualMachineScaleSetName = Get-NrpResourceName
$nwName = Get-NrpResourceName
$location = Get-PilotLocation
$resourceTypeParent = "Microsoft.Network/networkWatchers"
$nwLocation = Get-ProviderLocation $resourceTypeParent
$nwRgName = Get-NrpResourceGroupName
$templateFileVMSS = (Resolve-Path ".\TestData\DeploymentVMSS.json").Path
$pcName = Get-NrpResourceName
$pcName3 = $pcName + "2"

try {
. ".\AzureRM.Resources.ps1"

# Create Resource group
New-AzResourceGroup -Name $resourceGroupName -Location "$location"

# Deploy resources
Get-TestResourcesDeploymentVMSS -rgn "$resourceGroupName"

#Get public IP address
$address = Get-AzPublicIpAddress -ResourceGroupName $resourceGroupName

# Create Resource group for Network Watcher
New-AzResourceGroup -Name $nwRgName -Location "$location"

# Get Network Watcher
$nw = Get-CreateTestNetworkWatcher -location $location -nwName $nwName -nwRgName $nwRgName

Wait-Seconds 600

#Get Vmss and Instances
$vmss = Get-AzVmss -ResourceGroupName $resourceGroupName -VMScaleSetName $virtualMachineScaleSetName

#Install networkWatcherAgent on Vmss and Vmss Instances
Add-AzVmssExtension -VirtualMachineScaleSet $vmss -Name "AzureNetworkWatcherExtension" -Publisher "Microsoft.Azure.NetworkWatcher" -Type "NetworkWatcherAgentWindows" -TypeHandlerVersion "1.4" -AutoUpgradeMinorVersion $True
Update-AzVmss -ResourceGroupName "$resourceGroupName" -Name $virtualMachineScaleSetName -VirtualMachineScaleSet $vmss

# Updating all VMSS instances with NW agent
$instances = Get-AzVMSSVM -ResourceGroupName "$resourceGroupName" -VMScaleSetName $vmss.Name
foreach ($item in $instances) {
Update-AzVmssInstance -ResourceGroupName "$resourceGroupName" -VMScaleSetName $vmss.Name -InstanceId $item.InstanceID
}

# Create Capture settiings for packet capture, its only applicable if we are pass continuousCapture as true/false
$c1 = New-AzPacketCaptureSettingsConfig -FileCount 2 -FileSizeInBytes 102400 -SessionTimeLimitInSeconds 60

#Create packet capture
# with Continuous Capture, if you are using continuousCapture, change it to local Path instead FilePath
$job3 = New-AzNetworkWatcherPacketCaptureV2 -NetworkWatcher $nw -Name $pcName3 -TargetId $vmss.Id -TargetType "azurevmss" -ContinuousCapture $false -CaptureSetting $c1 -LocalPath C:\captures\Capture.cap -AsJob
$job3 | Wait-Job

Start-TestSleep -Seconds 2

#Get packet capture
$job3 = Get-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName3 -AsJob
$job3 | Wait-Job
$pc3 = $job3 | Receive-Job

#Write-Output ("PC3: '$pc3'")

#Verification
Assert-AreEqual $pc3.Name $pcName3
Assert-AreEqual "Succeeded" $pc3.ProvisioningState
Assert-AreEqual 2 $c1.FileCount
Assert-AreEqual 102400 $c1.FileSizeInBytes
Assert-AreEqual 60 $c1.SessionTimeLimitInSeconds
Assert-Null $pc3.TotalBytesPerSession
Assert-Null $pc3.TimeLimitInSeconds
Assert-AreEqual $pc3.TargetType AzureVMSS

#Stop packet capture
$job3 = Stop-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName3 -AsJob
$job3 | Wait-Job

#Remove packet capture
$job3 = Remove-AzNetworkWatcherPacketCapture -NetworkWatcher $nw -PacketCaptureName $pcName3 -AsJob
$job3 | Wait-Job
}
finally {
# Cleanup
Clean-ResourceGroup $resourceGroupName
#Clean-ResourceGroup $nwRgName
}
}

<#
.SYNOPSIS
Test Troubleshoot API.
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/Network/Network/Az.Network.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,8 @@ CmdletsToExport = 'Add-AzApplicationGatewayAuthenticationCertificate',
'New-AzNetworkWatcherPacketCaptureV2',
'New-AzNetworkWatcherProtocolConfiguration',
'New-AzO365PolicyProperty', 'New-AzOffice365PolicyProperty',
'New-AzP2sVpnGateway', 'New-AzPacketCaptureFilterConfig',
'New-AzP2sVpnGateway', 'New-AzPacketCaptureFilterConfig',
'New-AzPacketCaptureSettingsConfig',
'New-AzPacketCaptureScopeConfig', 'New-AzPrivateDnsZoneConfig',
'New-AzPrivateDnsZoneGroup', 'New-AzPrivateEndpoint',
'New-AzPrivateEndpointIpConfiguration', 'New-AzPrivateLinkService',
Expand Down
8 changes: 6 additions & 2 deletions src/Network/Network/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@
--->

## Upcoming Release
* Added a new command which creates an object for CaptureSetting, and added properties 'FileCount', 'FileSizeInBytes', and 'SessionTimeLimitInSeconds', which helps to configure the capture setting for packet capture as well as support for it for the following cmdlets:
- `New-AzPacketCaptureSettingsConfig`
* Added properties 'ContinuousCapture', 'LocalPath', and 'CaptureSetting' reference in Packet capture V2 command, as well as support for it for the following cmdlets:
- `New-AzNetworkWatcherPacketCaptureV2`
* Onboarded Application Gateway WAF Exceptions cmdlet.
- `New-AzApplicationGatewayFirewallPolicyException`
- `New-AzApplicationGatewayFirewallPolicyException`

## Version 7.17.0
* Added properties 'PublicIpAddressesV6', 'PublicIpPrefixesV6', and 'SourceVirtualNetwork' to NatGateway, as well as support for it for the following cmdlets:
Expand Down Expand Up @@ -1443,4 +1447,4 @@
- New-AzApplicationGatewaySslCertificate
- Set-AzApplicationGatewaySslCertificate
- New-AzApplicationGateway cmdlet updated with optional parameter -UserAssignedIdentityId, -UserAssignedIdentity
* Add MaxCapacity property in ApplicationGatewayAutoscaleConfiguration
* Add MaxCapacity property in ApplicationGatewayAutoscaleConfiguration
12 changes: 12 additions & 0 deletions src/Network/Network/Models/PSGetPacketCaptureResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public class PSGetPacketCaptureResult : PSChildResource
[JsonProperty(Order = 1)]
public List<PSPacketCaptureFilter> Filters { get; set; }

[JsonProperty(Order = 2)]
public bool? ContinuousCapture { get; set; }

[JsonProperty(Order = 3)]
public PSPacketCaptureSettings CaptureSettings { get; set; }

[JsonIgnore]
public string FiltersText
{
Expand All @@ -84,6 +90,12 @@ public string PacketCaptureErrorText
get { return JsonConvert.SerializeObject(this.PacketCaptureError, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}

[JsonIgnore]
public string CaptureSettingsText
{
get { return JsonConvert.SerializeObject(this.CaptureSettings, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}

public bool ShouldSerializePacketCaptureError()
{
return !string.IsNullOrEmpty(this.Name);
Expand Down
12 changes: 12 additions & 0 deletions src/Network/Network/Models/PSPacketCaptureResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ public class PSPacketCaptureResult : PSChildResource
[JsonProperty(Order = 1)]
public List<PSPacketCaptureFilter> Filters { get; set; }

[JsonProperty(Order = 2)]
public bool? ContinuousCapture { get; set; }

[JsonProperty(Order = 3)]
public PSPacketCaptureSettings CaptureSettings { get; set; }

[JsonIgnore]
public string FiltersText
{
Expand All @@ -64,6 +70,12 @@ public string StorageLocationText
get { return JsonConvert.SerializeObject(this.StorageLocation, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}

[JsonIgnore]
public string CaptureSettingsText
{
get { return JsonConvert.SerializeObject(this.CaptureSettings, Formatting.Indented, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); }
}

public bool ShouldSerializeStorageLocation()
{
return !string.IsNullOrEmpty(this.Name);
Expand Down
43 changes: 43 additions & 0 deletions src/Network/Network/Models/PSPacketCaptureSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Network.Models
{
using Microsoft.WindowsAzure.Commands.Common.Attributes;
using Newtonsoft.Json;

public class PSPacketCaptureSettings
{
/// <summary>
/// Gets or sets number of file count.
/// </summary>
[JsonProperty(Order = 1)]
[Ps1Xml(Target = ViewControl.Table)]
public int? FileCount { get; set; }

/// <summary>
/// Gets or sets number of bytes captured per packet.
/// </summary>
[JsonProperty(Order = 2)]
[Ps1Xml(Target = ViewControl.Table)]
public long? FileSizeInBytes { get; set; }

/// <summary>
/// Gets or sets maximum duration of the capture session in seconds.
/// </summary>
[JsonProperty(Order = 3)]
[Ps1Xml(Target = ViewControl.Table)]
public int? SessionTimeLimitInSeconds { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/Network/Network/Models/PSStorageLocation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,9 @@ public class PSStorageLocation
[JsonProperty(Order = 2)]
[Ps1Xml(Target = ViewControl.Table)]
public string FilePath { get; set; }

[JsonProperty(Order = 2)]
[Ps1Xml(Target = ViewControl.Table)]
public string LocalPath { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ public override void Execute()
}
pcResult.PacketCaptureStatus = packetCaptureStatus.PacketCaptureStatus;
pcResult.StopReason = packetCaptureStatus.StopReason;
if (psPacketCapture.ContinuousCapture != null)
{
pcResult.ContinuousCapture = psPacketCapture.ContinuousCapture;
pcResult.CaptureSettings = psPacketCapture.CaptureSettings;
}
}
catch
{
Expand Down Expand Up @@ -183,6 +188,11 @@ public override void Execute()
}
pcResult.PacketCaptureStatus = psPacketCaptureStatus.PacketCaptureStatus;
pcResult.StopReason = psPacketCaptureStatus.StopReason;
if (psPacketCapture.ContinuousCapture != null)
{
pcResult.ContinuousCapture = psPacketCapture.ContinuousCapture;
pcResult.CaptureSettings = psPacketCapture.CaptureSettings;
}
}
catch
{
Expand Down
Loading