Hello Scripting Community,
Normally I'd start out with a bit of an introduction but I'm pretty pressed for time on a small issue I am having. I am lead to believe that what I want to do is completely possible, but I lack the experience or knowledge to accomplish what it is I am trying to do. This is what has led me to post here.
Also before you spam me with "Exchange 2010 forum that way ->" or "learn to search" messages, while the issue I'm having is exchange related, I am confident that what I am looking to do is within the general realm of PS scripting. As well I have spent a great deal of time searching but am not finding what I'm looking for.
So with that, here is my issue... I have a need to update the "DeliverToMailboxAndForward" and "ForwardingAddress" option on over 600 AD user accounts. The exchange PS command I'm using is "Set-Mailbox". Also, I don't think I need to state this, but just in case, I am running all of these commands/scripts from the exchange servers "Exchange Management Shell".
What I have been given is a csv file called "TEST_USERS.csv" that contains the following fields:
contactaddress,firstname,lastname,existingaddress
peter.griffin@newdomain.com,peter,griffin,peter.griffin@domain.com
meg.griffin@newdomain.com,meg,griffin,meg.griffin@domain.com
The PS script I ended up creating (through quite a bit of research a la the search feature):
Import-csv c:\scripts\TEST_USERS.csv | foreach { $contactaddress = $_.Contactaddress $existingaddress = $_.Existingaddress Set-Mailbox -Identity $existingaddress -DeliverToMailboxAndForward $false -ForwardingAddress $contactaddress }
Now here is where things get tricky... Peter Griffin's account is in the parent.domain.local AD domain, Meg Griffin's account is in the child.parent.domain.local domain. The exchange server is authenticated in the parent.domain.local AD domain.
As it is, this command works fine and updates Peter Griffin's info perfectly, however the script was failing with the following error:
[PS] C:\Scripts>.\Enable_Forwarders_TEST.ps1 The operation couldn't be performed because object 'meg.griffin@domain.com' couldn't be found on 'dc01.parent.domain.local'.+ CategoryInfo : NotSpecified: (0:Int32) [Set-Mailbox], ManagementObjectNotFoundException+ FullyQualifiedErrorId : 29320F59,Microsoft.Exchange.Management.RecipientTasks.SetMailbox
After doing a bit of digging I was able to figure out that by modifying the script to include the -DomainController option in the Set-Mailbox command, and targeting it to a DC for the child.parent.domain.local I was able to successfully update Megs information.
Import-csv c:\scripts\TEST_USERS.csv | foreach { $contactaddress = $_.Contactaddress $existingaddress = $_.Existingaddress Set-Mailbox -Identity $existingaddress -DeliverToMailboxAndForward $false -ForwardingAddress $contactaddress -DomainController dc01.child.domain.local }
Note: At this point I had removed Peter from the CSV file so no error was returned.
So this has left me with a bit of a head scratcher and why I am posting this in the general Power Shell question area and not the Exchange specific one. Given the fields that I have available in my csv file, I need to figure out which domain a user is in, and based off of those results, point the Set-Mailbox command to query to a specific DC.
I have come up with a few possible options but as I am still at a beginner level with PS, and due to the limited time frame I have to try to get this to work, I thought I'd reach out to the community for help. In no particular order, here
are the options/pseudo code that I came up with. By all means this isn't the only way to do it but I figured I'd run these by the experts and see what happens:
Option 1 - Run a search on the user before modifying the field
- Perform a search in the entire AD forest for the user by using their email address
- Determine the users domain
- If the domain matches parent.domain.local then set the -DomainController field to dc01.parent.domain.local
- If the domain matches child.parent.domain.local then set the -DomainController field to dc01.child.parent.domain.local
Option 2 - (Similar to previous option, however this involves populating a new column in the CSV file)
- Perform a search in the entire AD forest for the user by using their email address
- Determine the users domain
- If the domain matches parent.domain.local then populate a new field in the CSV file to dc01.parent.domain.local
- If the domain matches child.parent.domain.local then populate a new field in the CSV file to dc01.child.parent.domain.local
Once populated, modify my script to import the new field into a variable and use it in the -DomainController field
Option 3 - Similar to option to, only the population of the new field is done by hand (not looking forward to this option)
I know this is a long shot, but I figure before I set off on the adventure of manually populating this field, I'd run it past the experts. My fear is that based off of the fields that I was given in the CSV, there isn't enough to go on. I do have the "Firstname" and "Lastname" fields that could help with any search queries but I'm not that experienced with PS to be able to tell quickly. The other thing is that I haven't found any good/solid information on how to search the "entire directory" for a user by using PS. As you can see, I have lots of questions, a willingness to learn but due to the short amount of time, need some guidance. Any help that anyone can provide would be extremely appreciated!
Cheers,
Ken