Solved

exchange, scripts

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

Expert Comment

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

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 15

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
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 …
This video discusses moving either the default database or any database to a new volume.

820 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