public/Enable-AzureAutomationDiagnostics.ps1
function Enable-AzureAutomationDiagnostics { [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 |