Powershell script for Exchange Server: Removing emails with cmdlets - powershell

I am writing a Powershell script for managing Exchange Distribution Groups and Public Folders.
In the Public Folder Management Console, I have created a Mail Enabled folder. In that folder's Properties, under the E-Mail Addresses tab, I want to remove all the occurrences of email addresses that contain "Correspondence" from the list.
My question is, how would you go about removing email addresses from a public folder's properties by using Powershell?
If you want to see a piece of code, here it is:
# create the new public folder
New-PublicFolder -Name $nextProjectName -Path "\Projets"
Add-PublicFolderClientPermission -Identity "\Projets\$nextProjectName" -AccessRights CreateItems, ReadItems, CreateSubfolders, EditOwnedItems, FolderVisible, DeleteOwnedItems -User $nextProjectName
New-PublicFolder -Name "Correspondance" -Path "\Projets\$nextProjectName"
Enable-MailPublicFolder -Identity "\Projets\$nextProjectName\Correspondance"
$correspondanceAlias = $nextProjectCode.Substring(1,6)
Set-MailPublicFolder -Identity "\Projets\$nextProjectName\Correspondance" -Alias "bccp$correspondanceAlias" -DisplayName "bccp$correspondanceAlias"
Here is the screenshot showing in detail what I want to remove. The two SMTP addresses and the second one of the two X400 addresses.

Not tested, but I think this should work:
foreach ($mailpf in get-mailpublicfolder){
$addrs = $mailpf.emailaddresses |
where {$_.proxyaddressstring -notmatch "smtp:.*correspondence.*"}
set-mailpublicfolder $mailpf.identity -emailaddresses $addrs -whatif
}

I have resolved the issue. All I did is to repeat the cmdlet that sets the properties of the public folder to add the email addresses, so the wrong email addresses never appear.
$correspondanceAlias = $nextProjectCode.Substring(1,6)
Set-MailPublicFolder -Identity "\Projets\$nextProjectName\Correspondance" -Alias "bccp$correspondanceAlias" -DisplayName "bccp$correspondanceAlias"
Set-MailPublicFolder -Identity "\Projets\$nextProjectName\Correspondance" -EmailAddresses "bccp$correspondanceAlias#matricis.local", "bccp$correspondanceAlias#matricis.com"

Related

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

Exchange online - Copy mailbox permissions from one user to another

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

Script to create multiple AD groups and set email (exchange is not used)

Powershell novice here. I need a script to create bulk AD groups and set the email address for the group. We do not use exchange. I have not been able to find good examples when not using exchange.
$Example = get-content c:\temp\Example.txt
foreach($Example in $Example){
New-ADGroup -Name "$Example.###" -SamAccountName "$Example.###" -Email "$Example.####Anywhere.com" -ParentContainer "OU=THERE,OU=Organization,DC=HERE,DC=NET" -GroupType "Security" -GroupScope "Global"
}
New-AdGroup doesn't have a parameter Email. You will have to use
-OtherAttributes #{mail = "$Example.####Anywhere.com"}
PS. If the dot after the variable leads to problems, you can also format like
-OtherAttributes #{mail = ('{0}.####Anywhere.com' -f $Example)}

New-mailbox script, with zipcode and P.O. Box values added to mailbox user account. possible?

I am using the following powershell code for creating new mailboxes in my organization.
$users = Import-CSV C:\mailboxes.csv
$users| foreach {
$Password = convertto-securestring $_.password -asplaintext -force
new-mailbox -name $_.name -alias $_.alias -FirstName $_.Firstname -LastName $_.Lastname -userPrincipalName $_.userPrincipalName -PrimarySmtpAddress $_.PrimarySmtpAddress -Database $_.database -RetentionPolicy "b3a83dc4-e471-4d05-b357-25535aa027af" -OrganizationalUnit $_.OrganizationalUnit -Password $Password –ResetPasswordOnNextLogon:$false
}
Is there a way to insert a static text/value to this "zip code" and "po box" boxes, on the new active directory user, created along with this mailboxes?
for example , zip code should contain: "0101010101" and P.O Box should contain "000"
Your assistance is most appreciated
One option is to use Set-ADUser from the ActiveDirectory module. At the beginning of your script (before any loops), you can run the following if you have the module available to your current session.
Import-Module ActiveDirectory
After your New-Mailbox command, you can add the Set-ADUser command:
Set-ADUser -Filter "UserPrincipalName -eq '$($_.userprincipalname)'" -PostalCode "01010101" -POBox "000"
Sometimes AD replication can cause inconsistencies with multiple commands against AD objects. To get around that, you would typically use the -Server parameter to consistently target a domain controller that will see all of your read and write operations. The alternative (a slower one) is to run the AD user modifications after all of the mailboxes have been created and data has replicated to the AD Site you would be targeting.
AdminOfThings - Thanks for your reply.
So tell me,
Considering your last comment about the AD User modification conflict that i might occur,
i`m thinking some sort of "time delay" code might resolve such issues.
would it be logical to add something like "Start-Sleep" command to add a delay between
the "new-mailbox" and "Set-ADUser" commands as you suggested?
if so can you...write down how my script should like exactly, adding all things together please?
Thanks.

Powershell - Batch Rename of Home Server in HomePath

Admittedly, I am not a PowerShell monster, so I'm going to punt...
I am working with a client who is pulling a list of all his user shares on his CIFS server to help redirect AD HomeDirectory paths in a major file server migration. This list is being compared to the list of AD users home directories as AD currently sees them.
The problem is that some user directories use old NT Usernames (NAMEI$) and some use SAMAACCOUNTNAME$. To Additionally complicate, the share SERVER differs in AD due to an elaborate history of DNS aliases over the past 10-15 years - so even though all the users home directories currently exist on SERVERA they could be mapped to OLDSERVER3, OLDERSERVER01, or OLDESTSERVERNT4 - resulting in home directories that are all over the map.
I need to write a script that can use the SAMACCOUNTNAME from a list, then change all the server information in the home directory to \NEWSEVERNAME\CURRENTSHARE$ - hopefully using something like this:
Use UserList
From UserList, get-ADuser -Identity $_ -HomeDrive "U:" -HomeDirectory
in HomeDirectory replace \\*\ with \\NewServer\ while leaving the Share$ untouched.
Set-ADuser -Identity $_ -HomeDrive "U:" -HomeDirectory
I'm fairly certain that this can be accomplished with regular expressions, for/each loops, etc... but I can't put it together.
Thank you for your help!
I went through the same migration a short while ago. Here is what you can use to set the new server while leaving the share folder untouched.
Import-Module activedirectory
$samAccountNameList = get-content "c:\userIds.txt"
$newServer = "newFps01"
foreach ($user in $samAccountNameList) {
$adProperties = get-aduser -Identity $user -Properties homeDirectory, homeDrive
$homeDrive = $adProperties.HomeDrive
# Split original homedirectory path and grab just the share folder portion
$shareFolder = ($adProperties.homeDirectory).Split("\")[3]
$newHomeDirectory = "\\$newServer\$shareFolder"
set-aduser -Identity $user -HomeDrive $homeDrive -HomeDirectory $newHomeDirectory
}