Exchange online - Copy mailbox permissions from one user to another - powershell

I'm Trying to find a way to copy a users mailbox permissions to another user, I can output the data I need in PS just can't find a way to then apply those permissions to the new user.
I'm not amazing with PS so please bare with me :)
Get-Mailbox -RecipientTypeDetails UserMailBox,SharedMailbox | Get-MailboxPermission -User
which then outputs the users permissions but I would like to be able to then add those permissions to my new user in the same script.

hope this helps:
$FromUser = Read-Host "Enter the email address of the user you want to copy mailbox permissions from"
$ToUser = Read-Host "Enter the email address of the user you want to set mailbox permissions for"
$Perm = Get-Mailbox | Get-MailboxPermission -User $FromUser
$Perm | ForEach-Object { $_
Add-MailboxPermission -Identity $_.Identity -AccessRights FullAccess -InheritanceType All -AutoMapping:$true -User $ToUser
Add-RecipientPermission -Identity $_.Identity -AccessRights SendAs -Confirm:$false -Trustee $oTUser
}
This will automatically find the permissions from User1 to User2. You can change the Parameters to whatever you want to put based on Microsofts allowed commands.
https://learn.microsoft.com/en-us/powershell/module/exchange/add-mailboxpermission?view=exchange-ps
https://learn.microsoft.com/en-us/powershell/module/exchange/add-recipientpermission?view=exchange-ps

Related

Export all disabled DL members to CSV

I have an issue I've not been able to work through and I'm hoping I can get assistance. I've taken over management of 15,000+ AD user accounts and almost 1500 o365 distribution groups. I have been trying to build a command or script to query all distribution groups and export a list of user accounts that are members and do not have a mailbox.
I was able to get a working script that will find and remove them all however it is keying of disabled user accounts which would remove members that should not be. I only need group members removed that do not have a mailbox in o365. Ideally, I'd like to query the groups and export the list of group members without a mailbox to a CSV and include Name, AccountName and AccountDisabled. Any assistance would be appreciated.
Failed attempt:
$dg = Get-DistributionGroup
foreach($group in $dg){
Get-DistributionGroupMember -Identity $group.identity | ?{$_.recipienttype -eq 'UserMailbox'} |
foreach{
$mbx = Get-Mailbox $_.alias
if($_.name -eq $mbx.name -and $mbx.AccountDisabled -eq $true){
write-host "Removing User:" $_.alias "from group:" $group.identity
remove-distributiongroupmember -Identity $group.Identity -Member $_.alias -Confirm:$false
Write-Host "User Successfully Removed"
}
}
}
This is the command I found which will output the user and group name to the screen and remove the group member however it is keying off disabled AD User accounts and it's outputting it in a format that won't export to csv.
Thank you
Pat

Remove-Mailbox Permissions in bulk

I've been asked to remove a user from mailbox full access permissions. I need this one for every mailbox in the company or specific users (around 180).
So that's what I did so far:
Remove-MailboxPermission -Identity "John Duo" -User "Nik Biessen" -AccessRights FullAccess -InheritanceType All
This removes the delegation rights from one user. I need the same thing just for all mailboxes or a list of them to be executed in one script.
Thanks in advance
You can easily do it using a foreach loop.
Get-Mailbox -ResultSize Unlimited |Foreach {Remove-MailboxPermission -Identity $_.samaccountname -User "Nik Biessen" -AccessRights FullAccess -InheritanceType All}
Hope Nik Biessen is the user who got access, which needs to be removed.
Please note that the csv file needs a header as ID in the first line.
Note - Code is not tested. Please test it before running it in a production environment.

Script to copy Exchange Distribution Groups from one user to another

I am hoping to get some help with a script to copy Exchange group permissions from one user to another. I currently have a script that works to copy mailbox permissions from one user to another but would like to expand it so that it can do Distribution Groups as well.
Connect-ExchangeOnline
$FUser = Read-Host "Enter the email address of the user you want to copy mailbox permissions from"
$TUser = Read-Host "Enter the email address of the user you want to set mailbox permissions for"
$GPerm = Get-Mailbox | Get-MailboxPermission -User $FUser
$GPerm | ForEach-Object { $_
Add-MailboxPermission -Identity $_.Identity -AccessRights FullAccess -InheritanceType All -User $TUser
Add-RecipientPermission -Identity $_.Identity -AccessRights SendAs -Confirm:$false -Trustee $TUser
}
While looking online I found a similar question online asked by someone else but their question was about coping the DL members from one to another DL.
Get-DistributionGroupMember -Identity "A" | % {add-distributiongroupmember -Identity "B" -Member $_.Name}
Additonally I was able to find a script working to remove the permissions for DLs. But didn't work if I changed the parts from remove to add. But the script isn't for what I am looking for as removing permissions and copying are two different things.
Thanks,
daaqis

Remove full access permissions of all disabled users on shared mailboxes with exchange management shell

I’m looking for a powershell exchange script to remove Full access permissions of all disabled users on all shared mailboxes in a specific OU.
This is what I got so far
Remove-MailboxPermission -Identity Sharedmailbox -AccessRights Fullaccess -InheritanceType all -user DisabledUser -Confirm:$false | where {$_.UseraccountControl -like "*accountdisabled*"}
Its seems to work but I’m not sure about the last piece of het script if it will check for “accountdisabled”
Then I created a variable so it will check only one specific OU
$ou = Get-ADUser -SearchBase "OU=Functional Mailboxes,OU=Generalaccounts,DC=DOMAIN,DC=COM" -Filter * foreach ($user in $ou)
Remove-MailboxPermission -Identity "$ou" -AccessRights Fullaccess -InheritanceType all -Confirm:$false | where {$_.UseraccountControl -like "*accountdisabled*"}
The script is checking the right OU but I'm still looking for the last part where it will automatically remove full access permissions of the disabled users ONLY.
Can someone show me the way?
Instead of trying to screen for disabled users after removing the mailbox permissions (which is what your Remove-MailboxPermission ... | Where-Object ... appears to be intended to do - except that the way you wrote it, it's only checking for disabled state after removing the permissions), try selecting for the disabled accounts first, then passing only the disabled accounts to Remove-MailboxPermission:
Get-ADUser -SearchBase ... -filter {Enabled -eq $false} | Remove-Mailbox ...
(replacing ... with the appropriate SearchBase or parameters for Remove-Mailbox, using $_ for the identity of the ADUser whose mailbox permissions you're removing.)

Exchange 2010: How can I check what permissions a user/mailbox has towards other mailboxes?

I know how to check who has Full Access or Send As permissions on a specific mailbox, but how can I check if a specific user has Full Access or Send As permissions on any mailbox?
By running Get-MailboxPermission cmdlet you can check which user/mailbox has what type of permissions to access other mailboxes in Exchange.
Check this helpful. And I'm sure it is what you was looking for.
http://exchangeserverpro.com/list-users-access-exchange-mailboxes/
And I also check this helpful
Get-Mailboxpermission for list of Mailboxes
This can be achieved by user the following powershell command:
Get-Mailbox | Get-MailboxPermission -User 'username'
The problem i run into that this doesn't include 'Security Groups' with mailbox permissions that a user might be member of.
If anyone knows how to solve this i would highly appreciate a reply.
Actually John Dane's answer is correct...it works for groups as well. The -User parameter accepts DistinguishedName or SamAccountName...both of which AD Security Groups have.
So just pass it the SamAccountName (or 'username') of your group and your golden. I used this to find out which mailbox an old group we were thinking about retiring had permissions to. I added a "| ft -autosize" to see the full identity field of the mailbox in the default output.
Get-Mailbox | Get-MailboxPermission -User 'SamAccountName'| ft -autosize
or just select the identity and access rights if that's all you need.
Get-Mailbox | Get-MailboxPermission -User 'SamAccountName'| select Identity,AccessRights | ft -autosize
With the following Command you don't have any missing entries:
Get-Mailbox -resultsize unlimited | Get-MailboxPermission | Where {(!$_.isinherited) -and ($_.user.SecurityIdentifier -ne "S-1-5-10") -and ($_.accessrights -contains "fullaccess") } | Select Identity,User | Export-Csv -Path "c:\temp\testmailboxpermissions.csv"