public/Enable-AzureAutomationDiagnostics.ps1
function Enable-AzureAutomationDiagnostics { <# .SYNOPSIS Enabling azure diagnostics logging for multiple automation accounts .DESCRIPTION Enable-AzureAutomationDiagnostics cmdlet enables azure diagnostics logging for all automation accounts, or a specific one, and sending the data to an oms workspace. The tool can be run by just specifying the subscription and workspace names, which will enable diagnostics logging for all automation accounts in that subscriptions, or by specifying a specific automation account name to the 'AutomationAccountName' parameter, which will only enable diagnostics logging for that specific automation account. .PARAMETER SubscriptionName Specify the subscription Name. .PARAMETER workspaceName Specify the OMS workspace name. .PARAMETER AutomationAccountName Specify the AutomationAccountname. .EXAMPLE Enable-AzureAutomationDiagnostics -subscriptionName <subscriptionName> -workspaceName <workspacename> -AutomationAccountName <AutomationAccountName> -Verbose #> [CmdletBinding( DefaultParameterSetName = 'All' )] Param ( [Parameter(Mandatory=$true, ParameterSetName = 'All')] [ValidateNotNullOrEmpty()] [Parameter(ParameterSetName = 'Individual')] [string]$subscriptionName, [Parameter(Mandatory=$true, ParameterSetName = 'All')] [ValidateNotNullOrEmpty()] [Parameter(ParameterSetName = 'Individual')] [string]$workspaceName, [Parameter(Mandatory=$false, ParameterSetName = 'Individual')] [string]$AutomationAccountName ) Process { # collection to return $output = @() # check to see if local token exists (ran Login-AzureRMAccount) if (($null -eq (Get-AzureRmContext).Account)) { Write-Warning "Please run < Login-AzureRMAccount > first to create a session token...exiting." break } # Get subscription name Try { Select-AzureRmSubscription -SubscriptionName $subscriptionName -ErrorAction Stop -Verbose | Out-Null } Catch { $error[0].Exception break } # resolve workspace Try { $workspace = (Get-AzureRmOperationalInsightsWorkspace -ErrorAction Stop -Verbose).Where({$_.Name -eq "$workspaceName"}) $workspaceID = $workspace.ResourceID if (($null -eq $workspaceID)) { Write-Warning "Failed to retreive OMS workspace, exiting" break } } Catch { } # Grab all automation accounts for default parameter set if (($PSCmdlet.ParameterSetName -eq 'All')) { Try { $automation_accounts = Find-AzureRmResource -ResourceType 'Microsoft.Automation/automationAccounts' -ErrorAction Stop -Verbose } Catch { $error[0].Exception break } } elseif (($PSCmdlet.ParameterSetName -eq 'Individual')) { Try { $automation_accounts = Find-AzureRmResource -ResourceNameEquals $AutomationAccountName -ErrorAction Stop -Verbose } Catch { } } else { throw 'Parameter validation failed, exiting.' break } # check if null/empty and warn if (($null -eq $automation_accounts)) { Write-Warning "No automation accounts returned, exiting" } # loop through automation accounts and enable diagnostics elseif (($null -ne $automation_accounts)) { foreach ($account in $automation_accounts) { Try { $op = Set-AzureRmDiagnosticSetting -ResourceId "$($account.resourceid)" -WorkspaceId $workspaceID -Enabled $true -ErrorAction Stop -Verbose $output += $op } Catch { $error[0].Exception } } } return $output } # end process block } # end function |