az/update-membership.ps1

75 lines
2.5 KiB
PowerShell
Raw Normal View History

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
$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) # 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 "Queue.HasValue!"
Write-Output "Messages..."
$Messages = $Queue.Value.ForEach{[Convert]::FromBase64String($_.MessageText)}.ForEach{[Text.Encoding]::UTF8.GetString($_)} # Decode the messages
Write-Output "Messages $(Measure-Object $Messages)!"