<# .SYNOPSIS Lets the EasyLife 365 app read an existing Log Analytics workspace in your subscription. .DESCRIPTION EasyLife 365 setup script. Use case: audit/audit-workspace (the audit log). Version: v1. Source: https://docs.easylife365.cloud/downloads/audit/audit-workspace/v1/grant.ps1 The script finds the Log Analytics workspace -WorkspaceName in the subscription you are signed in to. Then it assigns the role "Log Analytics Reader" to the service principal of the EasyLife 365 app (-AppId), on the workspace only. It creates nothing else. Each EasyLife 365 product has its own app. For a workspace that several products share, run the script once with the app ID of each product. You can run it again: a role that is assigned already is left as it is and listed as skipped. The script never changes the settings of a resource and never deletes anything. Run it as someone who can give access on the workspace: Owner, User Access Administrator or Role Based Access Control Administrator. Needs PowerShell 7 or Windows PowerShell 5.1 with the modules Az.Accounts and Az.Resources, signed in with Connect-AzAccount. Azure Cloud Shell has everything. .PARAMETER WorkspaceName The workspace. .PARAMETER AppId The application (client) ID of the EasyLife 365 app in your tenant. EasyLife 365 Admin shows it in the command. .PARAMETER ResourceGroupName Optional. When several workspaces in the subscription have the name, the resource group of the one you mean. .PARAMETER Subscription Optional. The subscription ID or name that holds the workspace. Without it the script uses the current subscription and asks you to confirm it, because workspace names are not unique in Azure. .PARAMETER Yes Optional. Uses the current subscription without asking. .EXAMPLE ./grant.ps1 -WorkspaceName contoso-audit -AppId 00000000-0000-0000-0000-000000000000 #> [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingWriteHost', '', Justification = 'The script talks to the person who runs it.')] param( [Parameter(Mandatory = $true)][string]$WorkspaceName, [Parameter(Mandatory = $true)][string]$AppId, [string]$ResourceGroupName, [string]$Subscription, [switch]$Yes ) $ErrorActionPreference = 'Stop' $role = 'Log Analytics Reader' if ($WorkspaceName -notmatch '^[A-Za-z0-9][A-Za-z0-9-]{2,61}[A-Za-z0-9]$') { throw '-WorkspaceName must be 4 to 63 letters, digits and hyphens, and start and end with a letter or digit.' } if ($AppId -notmatch '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$') { throw '-AppId must be the application (client) ID, a GUID such as 00000000-0000-0000-0000-000000000000.' } $context = Get-AzContext if (-not $context) { throw 'Sign in first with Connect-AzAccount.' } if ($Subscription) { $context = Set-AzContext -Subscription $Subscription -Scope Process } Write-Host "Subscription: $($context.Subscription.Name) ($($context.Subscription.Id))" if (-not $Subscription -and -not $Yes) { $answer = Read-Host "Give access on a workspace named $WorkspaceName in this subscription? [y/N]" if ($answer -ne "y") { throw "Stopped. Nothing was changed. Pass -Subscription to choose another subscription." } } $servicePrincipal = Get-AzADServicePrincipal -ApplicationId $AppId -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $servicePrincipal) { throw "Stopped. Nothing was changed. There is no service principal for the app $AppId in this tenant. Check that you're signed in to the right tenant, and that an administrator has consented to the EasyLife 365 app." } Write-Host "EasyLife 365 app: $($servicePrincipal.DisplayName), app ID $AppId, service principal $($servicePrincipal.Id)" # Workspace names are unique per resource group only, so the same name can exist more than once. $found = @(Get-AzResource -ResourceType 'Microsoft.OperationalInsights/workspaces' -Name $WorkspaceName) if ($ResourceGroupName) { $found = @($found | Where-Object { $_.ResourceGroupName -eq $ResourceGroupName }) } if ($found.Count -eq 0) { throw "Stopped. Nothing was changed. There is no workspace $WorkspaceName in this subscription. Pass -Subscription with the subscription that holds it." } if ($found.Count -gt 1) { throw "Stopped. Nothing was changed. There are $($found.Count) workspaces named $WorkspaceName in this subscription. Pass -ResourceGroupName with the resource group of the one you mean." } $scope = $found[0].ResourceId $assignment = Get-AzRoleAssignment -ObjectId $servicePrincipal.Id -RoleDefinitionName $role -Scope $scope | Where-Object { $_.Scope -eq $scope } | Select-Object -First 1 Write-Host '' if ($assignment) { Write-Host "Skipped: role $role for the EasyLife 365 app $AppId on the workspace (assigned)" Write-Host 'Nothing changed: the access was in place already.' } else { New-AzRoleAssignment -ObjectId $servicePrincipal.Id -RoleDefinitionName $role -Scope $scope | Out-Null Write-Host "Created: role $role for the EasyLife 365 app $AppId on the workspace" } Write-Host '' Write-Host "Workspace: $scope" Write-Host 'Next: go back to EasyLife 365 Admin and test the connection. A new role can take a few minutes to apply.'