2024-11-21 16:49:13 +00:00
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)
2024-11-21 14:52:30 +00:00
$null = Disable-AzContextAutosave -Scope Process
2024-11-21 16:49:13 +00:00
$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
2024-11-21 14:52:30 +00:00
# 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... "
2024-11-21 16:49:13 +00:00
$Queue = $StorageQueue . PeekMessages ( $MaxMessages ) # TODO: replace with $StorageQueue.GetMessages($MaxMessages) when ready to process in prod
2024-11-21 14:52:30 +00:00
Write-Output " QueueClient.PeekMessages! "
if ( -NOT $Queue . HasValue -or $Queue . Value . Count -eq 0 ) {
2024-11-21 16:49:13 +00:00
Write-Output " No messages to process. "
return
2024-11-21 14:52:30 +00:00
}
Write-Output " Queue.HasValue! "
Write-Output " Messages... "
2024-11-21 16:49:13 +00:00
$Messages = $Queue . Value . ForEach { [ Convert ] :: FromBase64String ( $_ . MessageText ) } . ForEach { [ Text.Encoding ] :: UTF8 . GetString ( $_ ) } # Decode the messages
2024-11-21 14:52:30 +00:00
Write-Output " Messages $( Measure-Object $Messages ) ! "