Answered by:
Output import results - using PS1 script

Question
-
I was asked to import over 1000 mail contacts which I was able to do... for the most part.
However, as a certain number of contacts were already in the global address list, there were errors.
This is because our mail contacts can have various statuses: Friend, Neighbor, Parent, etc. (just making those up, OK?)
So, if a contact is already included as a "friend" (and located in the "Friends" OU), they cannot be imported again, at least not with the very same SMTP address, as a "neighbor" in the "Neighbors" OU.
I want to output the operations that fill the screen of EMS as they are performed, but take up so much space that I can only see the last operations performed (first in, first out).
So just to make sure I get all the errors listed, I want to output this to a file as well.
Here's my script (just below).
Import-CSV "F:\Scripts\NewContacts.csv" | ForEach {
New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/EmailContacts"
}Where should I put...
out-file C:\ImportOp1.txt
???
Inside brackets, right? And just at the end?
But wait, I don't want PS to do this once for each entry. I want ONE .txt file displaying the overall results of the operation.
So outside the brackets?
Would test this myself on a practice server but only have a E2K3 machine available for the moment.
Might try a small test group on the production server but thought I'd ask here too. Don't like "testing" too much on a production server...
Tuesday, September 20, 2011 7:19 PM
Answers
-
Inside the curly braces you can add a semicolon to the end of the command that's in there and add a second command that executes afterward, or you can put the next command on the next line. However that Out-File won't do what you want it to do. What you want instead is the following.
Import-CSV "F:\Scripts\NewContacts.csv" | ForEach { New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/EmailContacts" 2>> C:\ImportOp1.txt }
The 2>> operator redirects the error stream, in this case appending the file.http://technet.microsoft.com/en-us/library/dd315283.aspx
Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."- Edited by Ed CrowleyMVP Tuesday, September 20, 2011 7:36 PM
- Marked as answer by David M (LePivert) Thursday, September 22, 2011 12:35 PM
Tuesday, September 20, 2011 7:35 PM
All replies
-
Inside the curly braces you can add a semicolon to the end of the command that's in there and add a second command that executes afterward, or you can put the next command on the next line. However that Out-File won't do what you want it to do. What you want instead is the following.
Import-CSV "F:\Scripts\NewContacts.csv" | ForEach { New-MailContact -Name $_.displayName -FirstName $_.givenName -LastName $_.sn -ExternalEmailAddress $_.mail -OrganizationalUnit "contoso.com/EmailContacts" 2>> C:\ImportOp1.txt }
The 2>> operator redirects the error stream, in this case appending the file.http://technet.microsoft.com/en-us/library/dd315283.aspx
Ed Crowley MVP "There are seldom good technological solutions to behavioral problems."- Edited by Ed CrowleyMVP Tuesday, September 20, 2011 7:36 PM
- Marked as answer by David M (LePivert) Thursday, September 22, 2011 12:35 PM
Tuesday, September 20, 2011 7:35 PM -
Hi,
Please update.
Thanks
Sophia XuThursday, September 22, 2011 2:13 AM -
It looks like adding thousands of contacts to the GAL is not the best choice:
http://social.technet.microsoft.com/Forums/en-US/exchangesvradmin/thread/22fde8ad-4fc4-4bac-bb52-188c84ea5b63
But the answer will be useful for my scripting in the future.
Thursday, September 22, 2011 12:37 PM