Skip to content

Instantly share code, notes, and snippets.

@owensengoku
Created February 20, 2020 19:38
Show Gist options
  • Save owensengoku/260fbdc55e0abffcd7cb886918466c94 to your computer and use it in GitHub Desktop.
Save owensengoku/260fbdc55e0abffcd7cb886918466c94 to your computer and use it in GitHub Desktop.
Import-Module AzureRm -Force
$TENANTID = '<REPLACE WITH YOUR AAD TENANT ID'
$SUBSCRIPTION_ID = '<REPLACE WITH YOUR SUBSCRIPTION ID>'
$SETTINGS_NAME = '<REPLACE WITH RANDOM NAME FOR DIAGNOSTIC SETTINGS NAME>'
$LA_RESOURCEGROUP_NAME = '<REPLACE WITH YOUR LOG ANALYTICS RESOURCE GROUP>'
$LA_WORKSPACE_NAME = '<REPLACE WITH YOUR LOG ANALYTICS WORKSPACE NAME>'
$LA_WORKSPACE_ID ='/subscriptions/{0}/resourcegroups/{1}/providers/microsoft.operationalinsights/workspaces/{2}' -f $SUBSCRIPTION_ID, $LA_RESOURCEGROUP_NAME, $LA_WORKSPACE_NAME
$clientId = '1b730954-1685-4b74-9bfd-dac224a7b894' #built-in client id for "azure powershell"
$redirectUri = 'urn:ietf:wg:oauth:2.0:oob' #redirectUri for built-in client
$graphUri = 'https://management.core.windows.net'
$authority = 'https://login.microsoftonline.com/{0}' -f $TENANTID
$authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext -ArgumentList $authority
$authResult = $authContext.AcquireToken($graphUri, $clientId, $redirectUri, "Always")
$token = $authResult.AccessToken
$uri = 'https://management.azure.com/providers/microsoft.aadiam/diagnosticSettings/{0}?api-version=2017-04-01-preview' -f $SETTINGS_NAME
$body = @{
id = "/providers/microsoft.aadiam/providers/microsoft.insights/diagnosticSettings/{0}" -f $SETTINGS_NAME
name = $SETTINGS_NAME
properties = @{
logs = @(
@{
category = "AuditLogs"
enabled = $true
retentionPolicy = @{
days = 0
enabled = $false
}
},
@{
category = "SignInLogs"
enabled = $true
retentionPolicy = @{
days = 0
enabled = $false
}
}
)
metrics = @()
workspaceId = $LA_WORKSPACE_ID
}
}
Invoke-WebRequest -Uri $uri -Body $(ConvertTo-Json $body -Depth 4) -Headers @{Authorization = "Bearer $token"} -Method Put -ContentType 'application/json'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment