Public/Invoke-AddGroupMember.ps1
function Invoke-AddGroupMember { <# .SYNOPSIS Adds user to a certain group .PARAMETER UserName Target user .PARAMETER GroupType Options are basic, standard, and mfa .EXAMPLE !add dk.test.hz basic|standard|mfa #> [PoshBot.BotCommand( CommandName = 'addgroup', Aliases = ('addlicense', 'member', 'license', 'group') )] [cmdletbinding()] param( [parameter(Mandatory)] [string]$username, [parameter(Mandatory)] [string]$grouptype ) Import-Module ActiveDirectory $searchname = '*' + $username + '*' try { $GCADUser = Get-ADUser -Filter { (((SamAccountName -like $searchname) -or (Name -like $searchname)) -and ((Enabled -eq $true) -and (mail -like "*"))) } -Server sundc1:3268 New-PoshBotCardResponse -Text ($GCADUser | Select-Object Name, UserPrincipalName, mail, telephonenumber, mobile, department | Out-String) } catch { Write-Output "$($_.Exception.Message)" Write-Output "$($_.error.message)" } if ($null -ne $GCADUser) { try { switch ($grouptype) { "basic" { $group = Get-ADGroup -Identity "sunssc.M365-business-basic.usg" Add-ADGroupMember -identity "sunssc.M365-business-basic.usg" -Members $GCADUser } "standard" { $group = Get-ADGroup -Identity "sunssc.M365-business-standard.usg" Add-ADGroupMember -identity "sunssc.M365-business-standard.usg" -Members $GCADUser } "mfa" { $group = Get-ADGroup -Identity "sunssc.Azure-MFA-users.usg" Add-ADGroupMember -identity "sunssc.Azure-MFA-users.usg" -Members $GCADUser } "voice" { $group = Get-ADGroup -Identity "sunssc.M365-business-voice.usg" Add-ADGroupMember -Identity "sunssc.M365-business-voice.usg" -Members $GCADUser } } New-PoshBotCardResponse -Text "Success: $GCADUser.UserPrincipalName has been added to $group.SamAccountName`n`nOn-premise Active Directory to Azure AD synchronization will be initiated after 30 seconds" Start-Sleep -seconds 30 $result = Invoke-Command -ComputerName "sun-aad.sunssc.local" -ScriptBlock { Start-ADSyncSyncCycle -PolicyType Delta } New-PoshBotTextResponse -Text ($result | Select-Object -Property PSComputerName, Result | out-string) } catch { Write-Output "$($_.Exception.Message)" Write-Output "$($_.error.message)" } } } |