<# .SYNOPSIS Creates a storage account and a container in your subscription and gives the EasyLife 365 app access to the container. .DESCRIPTION EasyLife 365 setup script. Use case: collab/guest-documents-storage (the documents your guests sign). Version: v1. Source: https://docs.easylife365.cloud/downloads/collab/guest-documents-storage/v1/create.ps1 In the subscription you are signed in to, the script: 1. looks up the service principal of the EasyLife 365 app (-AppId) in your tenant; 2. creates the resource group -ResourceGroupName in -Region, when the storage account and the group don't exist yet; 3. creates the storage account -AccountName: StorageV2, locally redundant, TLS 1.2, HTTPS only, Microsoft Entra ID sign-in only (no shared key), no anonymous blob access, no replication to other tenants; 4. creates the container -ContainerName, with no anonymous access; 5. assigns the role "Storage Blob Data Contributor" to the service principal, on the container only. You can run it again: whatever exists already is left as it is and listed as skipped. The script never changes the settings of an existing resource and never deletes anything. Run it as someone who can create resources in the subscription and give access there: Owner, or Contributor together with User Access Administrator or Role Based Access Control Administrator. Needs PowerShell 7 or Windows PowerShell 5.1 with the modules Az.Accounts, Az.Resources and Az.Storage, signed in with Connect-AzAccount. Azure Cloud Shell has everything. .PARAMETER AccountName The storage account: 3 to 24 lowercase letters and digits, unique in Azure. .PARAMETER ContainerName The container: 3 to 63 lowercase letters, digits and hyphens, starting and ending with a letter or digit, with no two hyphens in a row. .PARAMETER Region The Azure region of a new storage account, for example westeurope. .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. The resource group of a new storage account. Default: rg-easylife365. Created in -Region when it's missing. .PARAMETER Subscription Optional. The subscription ID or name. Without it the script uses the current subscription and asks you to confirm it. .PARAMETER Yes Optional. Uses the current subscription without asking. .EXAMPLE ./create.ps1 -AccountName contosoguests -ContainerName guest-documents -Region westeurope -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]$AccountName, [Parameter(Mandatory = $true)][string]$ContainerName, [Parameter(Mandatory = $true)][string]$Region, [Parameter(Mandatory = $true)][string]$AppId, [string]$ResourceGroupName = 'rg-easylife365', [string]$Subscription, [switch]$Yes ) $ErrorActionPreference = 'Stop' $role = 'Storage Blob Data Contributor' $created = New-Object System.Collections.Generic.List[string] $skipped = New-Object System.Collections.Generic.List[string] # Every value is checked before anything is sent to Azure. if ($AccountName -cnotmatch '^[a-z0-9]{3,24}$') { throw '-AccountName must be 3 to 24 lowercase letters and digits.' } if ($ContainerName -cnotmatch '^[a-z0-9][a-z0-9-]{1,61}[a-z0-9]$' -or $ContainerName.Contains('--')) { throw '-ContainerName must be 3 to 63 lowercase letters, digits and hyphens, start and end with a letter or digit, and have no two hyphens in a row.' } if ($Region -notmatch '^[A-Za-z0-9]{2,40}$') { throw '-Region must be the name of an Azure region, for example westeurope.' } 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.' } if ($ResourceGroupName -notmatch '^[-\w\._\(\)]{1,90}$') { throw '-ResourceGroupName is not a valid resource group name.' } $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 'Create the resources in this subscription? [y/N]' if ($answer -ne 'y') { throw 'Stopped. Nothing was created. Pass -Subscription to choose another subscription.' } } # The service principal exists once an administrator has consented to the EasyLife 365 app in your tenant. $servicePrincipal = Get-AzADServicePrincipal -ApplicationId $AppId -ErrorAction SilentlyContinue | Select-Object -First 1 if (-not $servicePrincipal) { throw "Stopped. Nothing was created. 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)" $account = Get-AzResource -ResourceType 'Microsoft.Storage/storageAccounts' -Name $AccountName | Select-Object -First 1 if ($account) { $skipped.Add("storage account $AccountName (exists in resource group $($account.ResourceGroupName))") } else { if (Get-AzResourceGroup -Name $ResourceGroupName -ErrorAction SilentlyContinue) { $skipped.Add("resource group $ResourceGroupName (exists)") } else { New-AzResourceGroup -Name $ResourceGroupName -Location $Region | Out-Null $created.Add("resource group $ResourceGroupName") } Write-Host "Creating the storage account $AccountName ..." New-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $AccountName -Location $Region ` -SkuName Standard_LRS -Kind StorageV2 -AccessTier Hot -MinimumTlsVersion TLS1_2 -EnableHttpsTrafficOnly $true ` -AllowBlobPublicAccess $false -AllowSharedKeyAccess $false -AllowCrossTenantReplication $false | Out-Null $created.Add("storage account $AccountName") $account = Get-AzResource -ResourceType 'Microsoft.Storage/storageAccounts' -Name $AccountName -ResourceGroupName $ResourceGroupName } $accountGroup = $account.ResourceGroupName $container = Get-AzRmStorageContainer -ResourceGroupName $accountGroup -StorageAccountName $AccountName -Name $ContainerName -ErrorAction SilentlyContinue if ($container) { $skipped.Add("container $ContainerName (exists)") } else { New-AzRmStorageContainer -ResourceGroupName $accountGroup -StorageAccountName $AccountName -Name $ContainerName -PublicAccess None | Out-Null $created.Add("container $ContainerName") } $scope = "$($account.ResourceId)/blobServices/default/containers/$ContainerName" $assignment = Get-AzRoleAssignment -ObjectId $servicePrincipal.Id -RoleDefinitionName $role -Scope $scope | Where-Object { $_.Scope -eq $scope } | Select-Object -First 1 if ($assignment) { $skipped.Add("role $role for the EasyLife 365 app on the container (assigned)") } else { New-AzRoleAssignment -ObjectId $servicePrincipal.Id -RoleDefinitionName $role -Scope $scope | Out-Null $created.Add("role $role for the EasyLife 365 app on the container") } Write-Host '' foreach ($item in $created) { Write-Host "Created: $item" } foreach ($item in $skipped) { Write-Host "Skipped: $item" } if ($created.Count -eq 0) { Write-Host 'Nothing changed: everything was in place already.' } Write-Host '' Write-Host "Container: $scope" Write-Host 'Next: go back to EasyLife 365 Admin and test the connection. A new role can take a few minutes to apply.'