Solved

exchange, scripts

Posted on 2016-10-10
3
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Expert Comment

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

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

751 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