[go: up one dir, main page]

0% found this document useful (0 votes)
236 views9 pages

Power Shell Command To Read A CSV File

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 9

When doing a bulk import of contacts you can use the following Powershell command to read a CSV file.

Import-Csv contacts.csv | ForEach { New-MailContact -Name $_.displayName -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.EmailNddress -OrganizationalUnit "YourDomain.Local/Contacts" } For an excellent list of what else you can import with the New-MailContact command (everything after the "|" character) you can go to the linked web site. As something I found out while trying to use this command you must have the same capitalization in _.Variable as you do for your column headers in the CSV file. Also you cannot have the CSV file open while you are doing your import.

Use the New-MailContact cmdlet to create a new mail-enabled contact. Syntax


New-MailContact -Name <String> -ExternalEmailAddress <ProxyAddress> [-Alias <String>] [ArbitrationMailbox <MailboxIdParameter>] [-Confirm [<SwitchParameter>]] [-DisplayName <String>] [-DomainController <Fqdn>] [-ExternalManaged <SwitchParameter>] [-FirstName <String>] [-Initials <String>] [-LastName <String>] [-MacAttachmentFormat <BinHex | UuEncode | AppleSingle | AppleDouble>] [-MessageBodyFormat <Text | Html | TextAndHtml>] [-MessageFormat <Text | Mime>] [-ModeratedBy <MultiValuedProperty>] [-ModerationEnabled <$true | $false>] [-Organization <OrganizationIdParameter>] [-OrganizationalUnit <OrganizationalUnitIdParameter>] [-PartnerObjectId <Guid>] [-PrimarySmtpAddress <SmtpAddress>] [-SendModerationNotifications <Never | Internal | Always>] [UsePreferMessageFormat <$true | $false>] [-WhatIf [<SwitchParameter>]]

Parameters Parameter Requi red Type Description

The ExternalEmailAddres ExternalEmailAddre Requir Microsoft.Exchange.Data.ProxyAddress s parameter specifies ss ed the target e-mail address. The Name parameter Requir specifies the common Name System.String ed name of the mail contact. The Alias parameter Option Alias System.String specifies the alias of al the mail contact. The ArbitrationMailbox Option Microsoft.Exchange.Configuration.Tasks.MailboxIdParame parameter specifies ArbitrationMailbox al ter the mailbox used to manage the moderation process. The Confirm switch Option causes the command Confirm System.Management.Automation.SwitchParameter al to pause processing and requires you to

acknowledge what the command will do before processing continues. You don't have to specify a value with the Confirm switch. The DisplayName parameter specifies Option DisplayName System.String the name displayed in al Microsoft Outlook for the mail contact. The DomainController parameter specifies the fully qualified Option domain name DomainController Microsoft.Exchange.Data.Fqdn al (FQDN) of the domain controller that writes this configuration change to Active Directory. The ExternalManaged Option switch specifies the ExternalManaged System.Management.Automation.SwitchParameter al mail contact as one managed by an external application. The FirstName Option parameter specifies FirstName System.String al the first name of the mail contact. The Initials parameter Option Initials System.String specifies the initials al of the mail contact. The LastName Option parameter specifies LastName System.String al the last name of the mail contact. The MacAttachmentForm at parameter specifies the Apple Macintosh operating system MacAttachmentFor Option Microsoft.Exchange.Data.Directory.Recipient.MacAttachm attachment format for mat al entFormat messages sent to the mail contact. The valid values for this parameter are:

BinHex UUENCODE AppleSingle AppleDouble

By default, this parameter is set to BinHex. The acceptable values for the MacAttachmentForm at parameter are dependent on the MessageFormat parameter. If the MessageFormat parameter is set to Text, you can only use BinHex or UUENCODE values for this parameter. If the MessageFormat parameter is set to Mime, you can only use BinHex, AppleSingle, or AppleDouble values for this parameter. The MessageBodyFormat parameter specifies the message body format for messages sent to the mail contact. The valid values for this parameter are: MessageBodyForma Option Microsoft.Exchange.Data.Directory.Recipient.MessageBod t al yFormat
Text Html TextAndHtml

By default, this parameter is set to TextAndHtml. The MessageFormat and MessageBodyFormat parameters are

interdependent. If the MessageFormat parameter is set to Mime, the MessageBodyFormat parameter can be set to any valid value. However, if the MessageFormat parameter is set to Text, the MessageBodyFormat parameter can only be set to Text. Therefore, if you want to set this parameter to Html or TextAndHtml, you must also set the MessageFormat parameter to Mime. The MessageFormat parameter specifies the message format for messages sent to the mail contact. The valid values for this parameter are:
Text Mime

MessageFormat

By default, this parameter is set to Option Microsoft.Exchange.Data.Directory.Recipient.MessageFor Mime. al mat The MessageFormat and MessageBodyFormat parameters are interdependent. If the MessageFormat parameter is set to Mime, the MessageBodyFormat parameter can be set to any valid value. However, if the MessageFormat parameter is set to

Text,

the MessageBodyFormat parameter can only be set to Text. Therefore, if you want to change the MessageFormat parameter from Mime to Text, you must also change the MessageBodyFormat parameter to Text. The ModeratedBy parameter specifies the users who are responsible for moderating the messages sent to this mailbox. To designate more than one user, separate the users with commas. This parameter is required if you set the ModerationEnabled parameter to $true. If you leave this parameter blank and there's a user already specified as the manager of this mailbox, the ModeratedBy parameter is automatically set by the ManagedBy parameter of the mailbox. Otherwise, an error is returned. The ModerationEnabled parameter enables or disables moderation for the mailbox. To enable moderation, set this parameter to $true. To disable moderation, set this parameter to $false.

ModeratedBy

Option Microsoft.Exchange.Data.MultiValuedProperty al

ModerationEnabled

Option System.Boolean al

The default value is $false. The Organization Option Microsoft.Exchange.Configuration.Tasks.OrganizationIdPa parameter is reserved Organization al rameter for internal Microsoft use. The OrganizationalUnit parameter specifies the organizational Option Microsoft.Exchange.Configuration.Tasks.OrganizationalUn OrganizationalUnit unit (OU) to which al itIdParameter the new contact is added, for example, redmond.contoso.com /contacts. This parameter is Option PartnerObjectId System.Guid reserved for internal al Microsoft use. The PrimarySmtpAddress parameter specifies the primary SMTP address for the mail contact. By default, the primary SMTP address is generated based on the default e-mail address policy. If you specify a primary SMTP PrimarySmtpAddres Option Microsoft.Exchange.Data.SmtpAddress address by using this s al parameter, the command sets the EmailAddressPolicy Enabled attribute of the mail contact to $false, and the email addresses of this mail contact aren't automatically updated based on e-mail address policies. The SendModerationNotifi cations parameter SendModerationNot Option Microsoft.Exchange.Data.Directory.Recipient.TransportMo specifies whether ifications al derationNotificationFlags status notifications are sent to users when a message they sent to the moderated

distribution group is rejected by one of the moderators. You can specify one of the following values:
Always Internal Never

Set this parameter to Always if you want notifications to be sent to all senders. Set this parameter to Internal if you want notifications to be sent only to the senders internal to your organization. Set this parameter to Never to disable all status notifications. The default value is Never. The UsePreferMessageFo rmat parameter specifies whether recipient preferred message format settings are used. When set to $true, this parameter specifies that the recipient preferred message format settings override the global settings for mail sent to this user. The WhatIf switch instructs the command to simulate the actions that it would take on the object. By using the WhatIf switch, you can view what

UsePreferMessageF Option System.Boolean ormat al

WhatIf

Option System.Management.Automation.SwitchParameter al

changes would occur without having to apply any of those changes. You don't have to specify a value with the WhatIf switch. Detailed Description The New-MailContact cmdlet creates a new mail contact object in Active Directory, and then mail-enables the mail contact. You need to be assigned permissions before you can run this cmdlet. Although all parameters for this cmdlet are listed in this topic, you may not have access to some parameters if they're not included in the permissions assigned to you. To see what permissions you need, see the "Recipient Provisioning Permissions" section in the Mailbox Permissions topic.

PS script:

Add-Content c:\debug.txt "::Starting Import: "; $err = ""; Import-Csv contacts.csv | ForEach { $output = "::"; $display = ""; if ($_."First Name") { $display = $_."First Name"; } if($_."Middle Name") { $display = $display + " " + $_."Middle Name"; } if($_."Last Name") { if ($_."First Name") { $display = $display + " " + $_."Last Name"; } else { $display = $_."Last Name"; } } $output = $output + "Importing: " + $display;

Add-Content c:\debug.txt $output; $otherfax = $_."Home Fax" + ", " + $_."Other Fax"; $otherphone = $_."Primary Phone" + ", " + $_."Business Phone 2" + ", " + $_."Car Phone" + ", " + $_."Callback" + ", " + $_."Company Main Phone" + ", " + $_."Other Phone"; $otherhome = $_."Home Phone 2" + ", " + ""; $businessstreet = $_."Business Street" + " " + $_."Business Street 2" + " " + $_."Business Street 3"; $postofficebox = $_."Business Address PO Box" + ", " + ""; New-MailContact -DomainController dc.domain.com -Name $display -Firstname $_."First Name" -Lastname $_."Last Name" -ExternalEmailAddress $_."E-mail Address" -OrganizationalUnit "domain.com/Contacts" -ErrorAction SilentlyContinue -ErrorVariable +err | Set-Contact -DomainController dc.domain.com -AssistantName $_."Assistant's Name" -City $_."Business City" -Company $_."Company" -Department $_."Department" -DisplayName $display -Fax $_."Business Fax" -HomePhone $_."Home Phone" -Initials $_."Initials" -MobilePhone $_."Mobile Phone" -Name $display -Notes $_."Notes" -Office $_."Location" -OtherFax $otherfax -OtherHomePhone $otherhome -OtherTelephone $otherphone -Pager $_."Pager" -Phone $_."Business Phone" -PostalCode $_."Business Postal Code" -PostOfficeBox $postofficebox -StateOrProvince $_."Business State" -StreetAddress $businessstreet -TelephoneAssistant $_."Assistant's Phone" -Title $_."Title" -WebPage $_."Web Page" -ErrorAction SilentlyContinue -ErrorVariable +err; Set-MailContact -Identity $display -DomainController dc.domain.com -CustomAttribute15 "Bristol" -ErrorAction SilentlyContinue -ErrorVariable +err; } Add-Content c:\debug.txt $err;

You might also like