Solved

exchange, scripts

Posted on 2016-10-10
3
60 Views
Last Modified: 2016-10-11
I am trying to run below command  on my exchange resource forest set up

Import-CSV CreateMailboxes-tt.csv | ForEach {

     New-Mailbox -Alias $_.alias -Name $_.name -FirstName $_.Givenname -LastName $_.Surname -userPrincipalName $_.UPN -Database “Field Mailbox Database” -OrganizationalUnit ' Email accounts' -Password $Password -LinkedDomainController "domain..COM" -LinkedMasterAccount "domain\$($_.Name)";

     Get-ADUser -Filter { UserPrincipalName -like $_.upn } | Disable-ADAccount;

SCRIPT RUNS FINE TILL HERE BUT THEN THROWS ERROR (ATTACHED) when adding distribution group

     Add-DistributionGroupMember -Identity "ActiveSync" -Member $_.upn;

     Add-DistributionGroupMember -Identity "OWA" -Member $_.upn;

}

I am running this csv file but I need to add additional email address to my exchange account domain-retail.com which I have to add manually after the user is created in exchange , is there anyw ay I can incorporate this email address domain-retail.com as primary addressERRORcreatemailboxes-tTt.csv
0
Comment
Question by:pramod1
  • 2
3 Comments
 
LVL 14

Expert Comment

by:Todd Nelson
ID: 41837588
Send me a private message in the morning.
0
 
LVL 38

Expert Comment

by:Adam Brown
ID: 41837678
You're trying to use $_.upn in a spot that doesn't pipe that value. Code should work better like this:

$mailboxes = Import-CSV CreateMailboxes-tt.csv 
ForEach ($box in $mailboxes) {
New-Mailbox -Alias $box.alias -Name $box.name -FirstName $box.Givenname -LastName $box.Surname -userPrincipalName $box.UPN -Database “Field Mailbox Database” -OrganizationalUnit ' Email accounts' -Password $Password -LinkedDomainController "domain.COM" -LinkedMasterAccount "domain\$($box.Name)";

Get-ADUser -Filter { UserPrincipalName -like $box.upn } | Disable-ADAccount;

Add-DistributionGroupMember -Identity "ActiveSync" -Member $box.upn;

Add-DistributionGroupMember -Identity "OWA" -Member $box.upn;

}

Open in new window


You can use the pipeline in the new-mailbox cmdlet because that's pulling data from your Import-csv cmdlet. But that attribute is overwritten when you pipe get-aduser to disable-adaccount. As a result, what you're trying to do is too complex to rely on the pipeline attribute $_.

To get around that problem, we can package the import-csv data into an attribute, then run foreach using the ($object in $array) method. This will process the entire CSV array object by creating a $box object for each line in the array, then running the commands in the Foreach scriptblock. With that technique, you can then call $box.upn and get the appropriate value without worrying about a broken pipeline.
0
 
LVL 14

Accepted Solution

by:
Todd Nelson earned 500 total points
ID: 41838783
Pramod,

 Good working with you.  Based on the conversation yesterday, you do not need to provide a password for linked mailboxes because they are created as disabled accounts.  So I've removed those items.  And I added the setting of a custom attribute to set the proper default reply address based on your email address policies.

Import-CSV CreateMailboxes-tt.csv | ForEach {

     New-Mailbox -Alias $_.alias -Name $_.name -FirstName $_.Givenname -LastName $_.Surname -userPrincipalName $_.UPN -Database “Field Mailbox Database” -OrganizationalUnit ' Email accounts' -LinkedDomainController "domain..COM" -LinkedMasterAccount "domain\$($_.Name)"

     Set-Mailbox -Identity $_.alias -CustomAttribute15 "StoreTablets"

     Add-DistributionGroupMember -Identity "ActiveSync" -Member $_.upn

     Add-DistributionGroupMember -Identity "StoreTabletAccts" -Member $_.upn

     Add-DistributionGroupMember -Identity "StoreTablets" -Member $_.upn
}

Open in new window



Good luck with your project.
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now