Quickly add #Azure diagnostics to #MSOMS


A quick one to add Azure Diagnostics (with the exception of the compute resource type) to Log Analytics workspace. For more information in what you’re about to do see.

install-module -Name AzureDiagnosticsAndLogAnalytics

login-azurermaccount
get-azurermsubscription | out-gridview -OutputMode Single | select-azurermsubscription
$stg=Get-AzureRmStorageAccount | out-gridview -OutputMode Single -Title 'Select the diagnostics storage account'
$scope= $MonitorableResourcesToOMSSolutions | Out-GridView -OutputMode Multiple -Title 'Diagnostics Scope'
$workspace = Find-AzureRmResource -ResourceType "Microsoft.OperationalInsights/workspaces" | out-gridview -OutputMode Single

$resources = (Get-AzureRmResource).Where({$_.ResourceType -in ($scope.Name) -and $_.Location -eq $stg.Location})

foreach ($resource in $resources) {
$result=Set-AzureRmDiagnosticSetting -ResourceId $resource.ResourceId -StorageAccountId $stg.Id -Enabled $true -RetentionEnabled $true -RetentionInDays 3
Add-AzureDiagnosticsToLogAnalytics -ResourceForLogs $resource -WorkspaceResource $workspace
}

foreach($s in $scope) {
$result=Set-AzureRmOperationalInsightsIntelligencePack -ResourceGroupName $workspace.ResourceGroupName -WorkspaceName $workspace.Name -intelligencepackname $s.Value -Enabled $true
}

** Update if you want to batch ingest Automation Accounts here you go

login-azurermaccount
get-azurermsubscription | out-gridview -Outputmode Single | select-azurermsubscription

$workspace = Find-AzureRmResource -ResourceType "Microsoft.OperationalInsights/workspaces" | out-gridview -OutputMode Single

$accounts = Find-AzureRmResource -ResourceType "Microsoft.Automation/automationAccounts" | out-gridview -OutputMode Multiple


foreach($account in $accounts) {

$StorageAccount = Find-AzureRmResource -ResourceType "Microsoft.Storage/storageAccounts" | where {$_.Location -ieq $account.Location} | out-gridview -OutputMode Single -Title ('Storage Account for {0}' -f $account.name)

if (!$StorageAccount) {

    # Make sure name of Storage account follows Storage naming rules
    $StorageAccountName = ($account.Name.ToLower() + "omsstorage") -creplace '[^a-z0-9 ]',''
    If($StorageAccountName.Length -gt 23) { $StorageAccountName = $StorageAccountName.substring(0,23) }

    # Check if storage account exists & create it if it does not
    Try {
        $StorageAccount = Get-AzureRMStorageAccount -StorageAccountName $StorageAccountName -ResourceGroupName $account.ResourceGroupName 
    }
    Catch 
    {
        Write-Verbose "Creating storage account $StorageAccountName for OMS logs."
        $StorageAccount = New-AzureRMStorageAccount -StorageAccountName $StorageAccountName -Location $account.Location -ResourceGroupName $account.ResourceGroupName -Type Standard_LRS
    }
}
else {
    $StorageAccount = Get-AzureRMStorageAccount -StorageAccountName $StorageAccount.Name -ResourceGroupName $account.ResourceGroupName 
}


# Enable diagnostics on the automation account to send logs to the storage account
Set-AzureRmDiagnosticSetting -ResourceId $account.ResourceId -StorageAccountId $StorageAccount.Id -Enabled $true -RetentionEnabled $true -RetentionInDays 7

Happy diagnostics ingestion.

Advertisements

  1. #Azure notable releases in the last 10 days | Quae Nocent Docent
  2. System Center Temmuz 2016 Bülten – Sertaç Topal

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: