Solved

exchange, scripts

Posted on 2016-10-10
3
47 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Not sure what the best email signature size is? Are you worried about email signature image size? Follow this best practice guide.
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.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

16 Experts available now in Live!

Get 1:1 Help Now