From a64ec2682484a146c9454a3ee76367775a54676b Mon Sep 17 00:00:00 2001 From: Nathan Windisch Date: Thu, 21 Nov 2024 16:49:13 +0000 Subject: [PATCH] Updated mock functions to only be installed when the real functions are not available, and updated variables to use AutomationVariables instead (via splatting) --- update-membership.ps1 | 73 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/update-membership.ps1 b/update-membership.ps1 index 5e4348d..c4ac342 100644 --- a/update-membership.ps1 +++ b/update-membership.ps1 @@ -1,24 +1,75 @@ -$ManagedIdentityClientId = "612223b3-b7ae-4433-b09f-480e9aef2287" -$SubscriptionName = "core" -$ResourceGroupName = "groupmanager-t-rg" -$StorageAccountName = "wndshgroupmanagertv1sa" -$MaxMessages = 32 +function Get-MockADGroup { + param ( + [Parameter(Mandatory)] + [string] + $Identity, + + [Parameter()] + [string[]] + $Property # Used for mocking, doesn't do anything + ) + + $MockGroups = Invoke-RestMethod https://pastebin.com/raw/DiiNqu25 + return [PSCustomObject[]]$MockGroups | Where-Object { $_.SID -eq $Identity -or $_.SamAccountName -eq $Identity } +} + +if (-NOT (Get-Command Add-ADGroupMember -ErrorAction SilentlyContinue)) { + function Add-ADGroupMember { + param ( + [Parameter(Mandatory, ValueFromPipeline)] + [PSCustomObject] + $InputObject, + + [Parameter(Mandatory)] + [string] + $SAMAccountName + ) + + Write-Output "Added $SAMAccountName to $($InputObject.SAMAccountName)" + } +} +if (-NOT (Get-Command Remove-ADGroupMember -ErrorAction SilentlyContinue)) { + function Remove-ADGroupMember { + param ( + [Parameter(Mandatory, ValueFromPipeline)] + [PSCustomObject] + $InputObject, + + [Parameter(Mandatory)] + [string] + $SAMAccountName + ) + + Write-Output "Removed $SAMAccountName to $($InputObject.SAMAccountName)" + } +} + +$MaxMessages = 32 # This is the service maximum (https://learn.microsoft.com/rest/api/storageservices/get-messages) $null = Disable-AzContextAutosave -Scope Process -$null = Connect-AzAccount -Identity -AccountId $ManagedIdentityClientId -Subscription $SubscriptionName -$null = Set-AzCurrentStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName +$ConnectAzAccount = @{ + Subscription = Get-AutomationVariable "SubscriptionName" + AccountId = Get-AutomationVariable "ManagedIdentityClientId" + Identity = $true +} +$null = Connect-AzAccount @ConnectAzAccount +$SetAzCurrentStorageAccount = @{ + ResourceGroupName = Get-AutomationVariable "ResourceGroupName" + Name = Get-AutomationVariable "StorageAccountName" +} +$null = Set-AzCurrentStorageAccount @SetAzCurrentStorageAccount # Get-AzStorageQueue complains (https://go.microsoft.com/fwlink?linkid=2258844), but it's the recommended method of getting messages... (https://learn.microsoft.com/azure/storage/queues/storage-powershell-how-to-use-queues#retrieve-a-queue) $null = Update-AzConfig -DisplaySecretsWarning $false $StorageQueue = (Get-AzStorageQueue -Name "members").QueueClient Write-Output "QueueClient.PeekMessages..." -$Queue = $StorageQueue.PeekMessages($MaxMessages) +$Queue = $StorageQueue.PeekMessages($MaxMessages) # TODO: replace with $StorageQueue.GetMessages($MaxMessages) when ready to process in prod Write-Output "QueueClient.PeekMessages!" if (-NOT $Queue.HasValue -or $Queue.Value.Count -eq 0) { - Write-Output "No messages to process." - return + Write-Output "No messages to process." + return } Write-Output "Queue.HasValue!" Write-Output "Messages..." -$Messages = $Queue.Value.ForEach{[Convert]::FromBase64String($_.MessageText)}.ForEach{[Text.Encoding]::UTF8.GetString($_)} +$Messages = $Queue.Value.ForEach{[Convert]::FromBase64String($_.MessageText)}.ForEach{[Text.Encoding]::UTF8.GetString($_)} # Decode the messages Write-Output "Messages $(Measure-Object $Messages)!" \ No newline at end of file