ActiveDirectory.
1 TopicPowerShell script to add users to multiple exchange server distribution list.
Newbie to PowerShell. Apologies for any Mistake in writing my requirements. Requirement is to add user to multiple On-prem AD-Group, With user prompts and after adding to single ad groups check for few software request which need conditional for example if a user is admin/contractor/external . Then check in ad group if the user is admin then remove from admin and add into external vice -versa. and if a user is contractor and not in external and admin, Then add user into admin. So that user should be populate in contractor and admin ad group. If the request type is null then it will just add the single ad groups where there is no need of apply conditions. I have tried multiple research and not able to get the requirements as per my need. And the software request will prompt as dictionary $requesttype = (@{"somevalue="admin"}) NOTE:- We are using PowerShell session which is connected with exchange server. This is how i am trying and it returns nothing just ask for user id and groups and after that it is stopped. $Session = .\Documents\testConnc.ps1 # for user email addresses $UserEmails = Read-Host "Enter user email addresses separated by commas (e.g., email address removed for privacy reasons,email address removed for privacy reasons)" # for distribution group names $GroupNames = Read-Host "Enter distribution group names separated by commas (e.g., Group1,Group2)" # for the request type $RequestType = @{ "basware" ="requester"; } # Split the user email addresses and group names into arrays $Groups = $GroupNames -split ',' # Loop through each user and each group and add the user to each group # Check if the request type is "basware" if ($RequestType -eq "basware") { # Check the user's AD group memberships $UserGroups = Get-ADPrincipalGroupMembership -Identity $UserEmail | Select-Object -ExpandProperty Name if ($UserGroups -contains "buyer") { # If the user is in the "buyer" group, remove them from "buyer" Remove-DistributionGroupMember -Identity "Basware_AD_GRoup" -Member $UserEmail -ErrorAction Stop Add-DistributionGroupMember -Identity "Basware_AD_GRoup" -Member $UserEmail -ErrorAction Stop Write-Host "User $UserEmail added to group successfully." } elseif ($UserGroups -contains "requester") { # If the user is in the "buyer" group, add them to "approver" Remove-DistributionGroupMember -Identity "Basware_AD_GRoup" -Member $UserEmail -ErrorAction Stop Add-DistributionGroupMember -Identity "Basware_AD_GRoup" -Member $UserEmail -ErrorAction Stop Write-Host "User $UserEmail added to group successfully." } } foreach ($GroupName in $Groups) { try{ # Add the user to the specified group Add-DistributionGroupMember -Identity $GroupName -Member $UserEmails -ErrorAction Stop Write-Host "User $UserEmail added to group $GroupName successfully." } catch { Write-Host "Error adding user $UserEmail to group $GroupName $_" } } #Remove-PSSession -Session $Session1.1KViews0likes2Comments