Skip to content

Instantly share code, notes, and snippets.

@jeffpatton1971
Created January 14, 2016 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffpatton1971/ae0b3bcd849e0638a871 to your computer and use it in GitHub Desktop.
Save jeffpatton1971/ae0b3bcd849e0638a871 to your computer and use it in GitHub Desktop.
A script that will enable diagnostics on network security groups. This will add the logs to a specific storage account.
Param
(
[string]$StorageAccountName,
[string]$StorageAccountResourceGroup
)
try
{
$ErrorActionPreference = "Stop"
$Error.Clear()
$StorageAccount = Get-AzureRmStorageAccount -ResourceGroupName $StorageAccountResourceGroup -Name $StorageAccountName
$NetworkSecurityGroups = Get-AzureRmNetworkSecurityGroup
foreach ($NetworkSecurityGroup in $NetworkSecurityGroups)
{
$DiagnosticSettings = Get-AzureRmDiagnosticSetting -ResourceId $NetworkSecurityGroup.Id
if ($DiagnosticSettings.StorageAccountId -eq $null)
{
if($NetworkSecurityGroup.ResourceGroupName.Contains($StorageAccountResourceGroup))
{
Set-AzureRmDiagnosticSetting -ResourceId $NetworkSecurityGroup.Id -StorageAccountId $StorageAccount.Id -Enabled $true -Categories 'NetworkSecurityGroupEvent','NetworkSecurityGroupRuleCounter'
}
}
}
}
catch
{
Write-Output $Error
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment