source/Public/Set-RJWorkplaceCloudStorage.ps1
|
function Set-RJWorkplaceCloudStorage { <# .SYNOPSIS Deploys the RealmJoin Workplace Cloud Storage account to Azure. .DESCRIPTION Deploys the storage account used by RealmJoin Workplace Cloud Storage (favorites, wallpapers, signatures, files) and grants the RealmJoin service principal the Storage Blob Data Contributor role on it. RealmJoin accesses the account with Microsoft Entra ID; no account key is used. An existing storage account (-StorageAccountName) is not redeployed; only the role is assigned. It must allow anonymous blob access. Uses bundled ARM templates (JSON). Prerequisites: - Az.Accounts and Az.Resources PowerShell modules - Resource group must already exist .PARAMETER ResourceGroupName The name of the resource group to deploy to (must exist). .PARAMETER StorageAccountName Optional. The name of the storage account (3-24 lowercase letters and digits). If not provided, a unique name is auto-generated. .PARAMETER SubscriptionId Optional. The Azure subscription ID to deploy to. If not provided, uses the current context. .PARAMETER SkuName Optional. The SKU of a newly created storage account. Default: Standard_LRS. .PARAMETER Token Optional. The RealmJoin onboarding token. When provided, registers the storage account with RealmJoin API. .PARAMETER WhatIf Shows what would be deployed without actually performing the deployment. .EXAMPLE Set-RJWorkplaceCloudStorage -ResourceGroupName "rg-realmjoin" .EXAMPLE Set-RJWorkplaceCloudStorage -ResourceGroupName "rg-realmjoin" -StorageAccountName "strjwcs01" .EXAMPLE Set-RJWorkplaceCloudStorage -ResourceGroupName "rg-realmjoin" -WhatIf Shows what resources would be deployed without actually deploying. .EXAMPLE Set-RJWorkplaceCloudStorage -ResourceGroupName "rg-realmjoin" -Token "your-token" Deploys and registers with the RealmJoin API. #> [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory)] [string]$ResourceGroupName, [ValidatePattern('^[a-z0-9]{3,24}$', Options = 'None')] [string]$StorageAccountName, [string]$SubscriptionId, [ValidateSet('Standard_LRS', 'Standard_ZRS', 'Standard_GRS', 'Standard_RAGRS', 'Standard_GZRS', 'Standard_RAGZRS')] [string]$SkuName = $DefaultWorkplaceCloudStorageSkuName, [string]$Token ) begin { Write-Verbose "Set-RJWorkplaceCloudStorage: Starting" } process { if (-not (Test-RJModuleVersion)) { # Module is outdated - exit gracefully without throwing return } # Initialize Az modules Write-Information "Initializing required Azure modules..." if (-not (Initialize-RequiredModule)) { Write-Verbose "Azure module initialization failed - cannot proceed" return } # Connect to Azure $connectParams = @{} if ($SubscriptionId) { $connectParams.SubscriptionId = $SubscriptionId } $context = Connect-RJAzureContext @connectParams if ($null -eq $context) { Write-Information "Operation cancelled." return } # Verify resource group $null = Test-ResourceGroup -ResourceGroupName $ResourceGroupName if ($PSCmdlet.ShouldProcess("Resource Group: $ResourceGroupName", "Deploy RealmJoin Workplace Cloud Storage")) { Write-Information "" Write-Information "============================================" Write-Information "RealmJoin Workplace Cloud Storage Deployment" Write-Information "============================================" Write-Information "" # Deploy $deployParams = @{ ResourceGroupName = $ResourceGroupName SkuName = $SkuName } if ($StorageAccountName) { $deployParams.StorageAccountName = $StorageAccountName } $result = Set-RJWorkplaceCloudStorageResource @deployParams if (-not $result.Success) { throw "Workplace Cloud Storage deployment failed" } # Optional API registration if ($Token) { Write-Information "" Write-Information "--- RealmJoin API Registration ---" $additionalData = @{ tenantId = $context.TenantId subscriptionId = $context.SubscriptionId resourceGroupName = $ResourceGroupName storageAccountName = $result.Outputs.StorageAccountName blobEndpoint = $result.Outputs.BlobEndpoint } try { Invoke-RJOnboarding -Token $Token -ApiUrl (Get-RealmJoinTenantWorkplaceCloudStorageConfigApiUrl) -AdditionalData $additionalData } catch { # The Azure deployment already succeeded. Surfacing this as a terminating error would make # the interactive retry loop redeploy and re-send the (single-use) onboarding token. Write-Warning "RealmJoin API registration failed: $($_.Exception.Message)" Write-Warning "The storage account was deployed successfully. Re-run with a new onboarding token to register it." } } # Summary Write-Information "" Write-Information "============================================" Write-Information "Deployment Complete!" Write-Information "============================================" Write-Information "Storage Account Name: $($result.Outputs.StorageAccountName)" Write-Information "Storage Account ID: $($result.Outputs.StorageAccountId)" Write-Information "Blob Endpoint: $($result.Outputs.BlobEndpoint)" Write-Information "" } else { # WhatIf: show analysis $analysis = Get-RJAzureResourceAnalysis -ResourceGroupName $ResourceGroupName ` -DeployWorkplaceCloudStorage $true ` -StorageAccountName $StorageAccountName ` -SkuName $SkuName Show-RJAzureResourceAnalysis -Analysis $analysis } } end { Write-Verbose "Set-RJWorkplaceCloudStorage: Completed" } } |