你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

使用 Azure PowerShell 创建、更改、启用、禁用或删除虚拟网络流日志

虚拟网络安全组流日志记录是 Azure 网络观察程序的一项功能,可用于记录有关流经 Azure 虚拟网络的 IP 流量的信息。 有关虚拟网络流日志记录的详细信息,请参阅虚拟网络流日志概述

在本文中,你将了解如何使用 Azure PowerShell 创建、更改、启用、禁用或删除虚拟网络流日志。 你可以了解如何使用 Azure 门户Azure CLI 管理虚拟网络流日志。

先决条件

注册 Insights 提供程序

必须注册 Microsoft.Insights 提供程序才能成功记录虚拟网络中的流量。 如果你不确定是否已注册 Microsoft.Insights 提供程序,请使用 Register-AzResourceProvider 来注册。

# Register Microsoft.Insights provider.
Register-AzResourceProvider -ProviderNamespace Microsoft.Insights

启用虚拟网络流日志

使用 New-AzNetworkWatcherFlowLog 创建虚拟网络流日志。

# Place the virtual network configuration into a variable.
$vnet = Get-AzVirtualNetwork -Name myVNet -ResourceGroupName myResourceGroup
# Place the storage account configuration into a variable.
$storageAccount = Get-AzStorageAccount -Name myStorageAccount -ResourceGroupName myResourceGroup

# Create a VNet flow log.
New-AzNetworkWatcherFlowLog -Enabled $true -Name myVNetFlowLog -NetworkWatcherName NetworkWatcher_eastus -ResourceGroupName NetworkWatcherRG -StorageId $storageAccount.Id -TargetResourceId $vnet.Id

启用虚拟网络流日志和流量分析

使用 New-AzOperationalInsightsWorkspace 创建流量分析工作区,然后使用 New-AzNetworkWatcherFlowLog 创建使用该工作区的虚拟网络流日志。

# Place the virtual network configuration into a variable.
$vnet = Get-AzVirtualNetwork -Name myVNet -ResourceGroupName myResourceGroup
# Place the storage account configuration into a variable.
$storageAccount = Get-AzStorageAccount -Name myStorageAccount -ResourceGroupName myResourceGroup

# Create a traffic analytics workspace and place its configuration into a variable.
$workspace = New-AzOperationalInsightsWorkspace -Name myWorkspace -ResourceGroupName myResourceGroup -Location EastUS

# Create a VNet flow log.
New-AzNetworkWatcherFlowLog -Enabled $true -Name myVNetFlowLog -NetworkWatcherName NetworkWatcher_eastus -ResourceGroupName NetworkWatcherRG -StorageId $storageAccount.Id -TargetResourceId $vnet.Id -EnableTrafficAnalytics -TrafficAnalyticsWorkspaceId $workspace.ResourceId -TrafficAnalyticsInterval 10

列出区域中的所有流日志

使用 Get-AzNetworkWatcherFlowLog 列出订阅中特定区域内的所有流日志资源。

# Get all flow logs in East US region.
Get-AzNetworkWatcherFlowLog -NetworkWatcherName NetworkWatcher_eastus -ResourceGroupName NetworkWatcherRG | format-table Name

查看虚拟网络流日志资源

使用 Get-AzNetworkWatcherFlowLog 查看流日志资源的详细信息。

# Get the flow log details.
Get-AzNetworkWatcherFlowLog -NetworkWatcherName NetworkWatcher_eastus -ResourceGroupName NetworkWatcherRG -Name myVNetFlowLog

下载流日志

若要从存储帐户下载虚拟网络流日志,请使用 Get-AzStorageBlobContent cmdlet。

虚拟网络流日志文件会保存到以下路径的存储帐户:

https://{storageAccountName}.blob.core.windows.net/insights-logs-flowlogflowevent/flowLogResourceID=/SUBSCRIPTIONS/{subscriptionID}/RESOURCEGROUPS/NETWORKWATCHERRG/PROVIDERS/MICROSOFT.NETWORK/NETWORKWATCHERS/NETWORKWATCHER_{Region}/FLOWLOGS/{FlowlogResourceName}/y={year}/m={month}/d={day}/h={hour}/m=00/macAddress={macAddress}/PT1H.json

注意

还可以使用 Azure 存储资源管理器从存储帐户容器访问和下载 VNet 流日志文件。 存储资源管理器是可用于访问和处理 Azure 存储数据的独立应用。 有关详细信息,请参阅存储资源管理器入门

对流日志资源禁用流量分析

若要对流日志资源禁用流量分析并继续生成虚拟网络流日志以及将其保存到存储帐户,请使用 Set-AzNetworkWatcherFlowLog

# Place the virtual network configuration into a variable.
$vnet = Get-AzVirtualNetwork -Name myVNet -ResourceGroupName myResourceGroup
# Place the storage account configuration into a variable.
$storageAccount = Get-AzStorageAccount -Name mynwstorageaccount -ResourceGroupName Storage

# Update the VNet flow log.
Set-AzNetworkWatcherFlowLog -Enabled $true -Name myVNetFlowLog -NetworkWatcherName NetworkWatcher_eastus -ResourceGroupName NetworkWatcherRG -StorageId $storageAccount.Id -TargetResourceId $vnet.Id

禁用虚拟网络流日志记录

若要在不删除虚拟网络流日志的情况下禁用该流日志,以便以后可以重新启用,请使用 Set-AzNetworkWatcherFlowLog

# Place the virtual network configuration into a variable.
$vnet = Get-AzVirtualNetwork -Name myVNet -ResourceGroupName myResourceGroup
# Place the storage account configuration into a variable.
$storageAccount = Get-AzStorageAccount -Name mynwstorageaccount -ResourceGroupName Storage

# Disable the VNet flow log.
Set-AzNetworkWatcherFlowLog -Enabled $false -Name myVNetFlowLog -NetworkWatcherName NetworkWatcher_eastus -ResourceGroupName NetworkWatcherRG -StorageId $storageAccount.Id -TargetResourceId $vnet.Id

删除虚拟网络流日志资源

若要删除虚拟网络流日志资源,请使用 Remove-AzNetworkWatcherFlowLog

# Delete the VNet flow log.
Remove-AzNetworkWatcherFlowLog -Name myVNetFlowLog -NetworkWatcherName NetworkWatcher_eastus -ResourceGroupName NetworkWatcherRG