Updated mock functions to only be installed when the real functions are not available, and updated variables to use AutomationVariables instead (via splatting)

This commit is contained in:
Nathan Windisch 2024-11-21 16:49:13 +00:00
parent f8b6ea8755
commit a64ec26824

View File

@ -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)!"