azure - 如何使用 Azure PowerShell 创建 Azure 网络安全组流日志
问题描述
我想为给定虚拟机的网络安全组创建一个 NSG 流日志,并使用 PowerShell 链接到给定的存储帐户。
我怎样才能做到这一点?
解决方案
这可以通过以下操作序列来实现:
- 确定链接到虚拟机的 NSG
- 获取或创建一个用于NSG位置的NetworkWatcher
- 找到合适的存储帐户
- 设置流日志配置,如果不存在
param(
# RegEx pattern to find your first VM in your current subscription
[Parameter(Mandatory = $true, Position = 1)]
[string]$vmNamePattern,
# RegEx pattern to find a storage account in your current subscription
[Parameter(Mandatory = $true, Position = 2)]
[string]$storageNamePattern
)
$vm = Get-AzVM | Where-Object { $_.Name -match $vmNamePattern } | Select-Object -First 1
$nic = Get-AzNetworkInterface -ResourceId $vm.NetworkProfile.NetworkInterfaces[0].Id
$sn = Get-AzVirtualNetworkSubnetConfig -ResourceId $nic.IpConfigurations[0].Subnet.Id
$nsgRes = Get-AzResource -ResourceId $sn.NetworkSecurityGroup.Id
$nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $nsgRes.ResourceGroupName -Name $nsgRes.ResourceName
# create or get NetworkWatcher
$nw = Get-AzNetworkWatcher -ResourceGroupName NetworkWatcherRg | ? { $_.Location -eq $nsg.Location }
if (!$nw) {
New-AzNetworkWatcher -ResourceGroupName NetworkWatcherRg -Location $nsg.Location -Name $("NetworkWatcher_" + $nsg.Location)
$nw = Get-AzNetworkWatcher -ResourceGroupName NetworkWatcherRg | ? { $_.Location -eq $nsg.Location }
}
# detect first viable storage account
$storageAccount = Get-AzStorageAccount | Where-Object { $_.StorageAccountName -match $storageNamePattern -and $_.PrimaryEndpoints.Blob -match "^http" } | Select-Object -First 1
# get or set NSG flow log if not yet established
$fl = Get-AzNetworkWatcherFlowLogStatus -NetworkWatcher $nw -TargetResourceId $nsg.Id
if (!$fl) {
# https://docs.microsoft.com/de-de/azure/network-watcher/network-watcher-nsg-flow-logging-powershell
Set-AzNetworkWatcherConfigFlowLog -NetworkWatcher $nw -TargetResourceId $nsg.Id -StorageAccountId $storageAccount.Id -EnableFlowLog $true -FormatType Json -FormatVersion 2
}
推荐阅读
- c++ - 在 Visual Studio Enterprise 2017 (C++) 中测量函数执行时间
- android-fragments - ParseUser 查询似乎在片段中不起作用
- php - 如何始终首先显示搜索结果中的特定帖子
- c# - 如何在 C# 中使用文件的相对路径
- r - 在命名列表中组合多个 checkboxInput 和 checkboxgroupinput 选择
- matlab - Matlab:如何在 matlabFunction 中指定输入?
- node.js - 如何解决您需要启用 javascript 才能在 React 应用程序中运行此应用程序的问题?
- java - 没有CALL_LOG权限无法获取Android P来电号码
- python - 如何在 python 中创建一个显示所有可用命令的命令?喜欢终端的帮助
- javascript - 如何使用函数 removeData 添加和删除嵌套数组“项目”中的数据