Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run powershell script?

Status
Not open for further replies.

jjjax64

IS-IT--Management
Apr 27, 2010
39
US
New to this but know how to get to Exchange 2010 power shell and have ruyn individual commands but found the sample script below but not sure how to run it and it looks to be looking for parameters so not sure how to pass them? Thanks, Joe

# Import-PFContacts
#
# The purpose of this script is to import contacts into a public folder on
# Exchange 2007 or 2010 from a CSV file. The script reads from a CSV and then
# creates a contact with the appropriate fields using the Exchange Web
# Services Managed API.
#
# Requirements:
#
# This script requires Powershell 2.0. If you get the following error then you do not
# have Powershell 2.0 installed:
#
# The term 'Import-Module' is not recognized as a cmdlet, function, operable program,
# or script file. Verify the term and try again.
#
# The script also requires the EWS managed API, which can be downloaded here:
# #
# Make sure the Import-Module command below matches the DLL location of the API.
#
# Syntax:
# .\Import-PFContacts admin@contoso.com C:\SomeFile.csv "Top Level Folder\Subfolder\Contacts Folder"
#
# The first parameter is the email address that we'll use to run autodiscovery
# and logon. The second parameter is the import file, and the third is the folder
# where we want to create the contacts.
#
# Below you will find details on the columns that are supported in the import
# file. Note that column names are CASE SENSITIVE.
#
# The import file can contain the following email address columns:
#
# EmailAddress1, EmailAddress2, EmailAddress3
#
# The import file can contain the following phone number columns:
#
# AssistantPhone, BusinessFax, BusinessPhone, BusinessPhone2, Callback, CarPhone,
# CompanyMainPhone, HomeFax, HomePhone, HomePhone2, Isdn, MobilePhone, OtherFax,
# OtherTelephone, Pager, PrimaryPhone, RadioPhone, Telex, TtyTddPhone
#
# The import file can contain the following physical address columns. Note that
# the syntax for these is a little odd at the moment. The address has five fields,
# which must be in order and separated by % characters. They are street, city,
# state, country or region, and postal code. Some of the fields can be left
# blank if desired. An example address entry might look like this:
# One Microsoft Way%Redmond%WA%US%98052
# The physical address columns are:
#
# Home, Business, Other
#
# The imort file can contain the following other columns:
#
# AssistantName, Birthday, BusinessHomePage, CompanyName, CompleteName, Department,
# DisplayName, FileAs, Generation, GivenName, Initials, JobTitle, Manager, MiddleName,
# Mileage, NickName, OfficeLocation, Profession, SpouseName

param([string]$autoDiscoverAddress, [string]$importFile, [string]$folderPath)

##########
#
# This path must match the install location of the EWS managed API. Change it if needed.
#
Import-Module -Name "C:\Program Files\Microsoft\Exchange\Web Services\1.0\Microsoft.Exchange.WebServices.dll"
#
##########

$emailAddressColumns = new-object System.Collections.Specialized.StringCollection
$foo = $emailAddressColumns.AddRange(@("EmailAddress1", "EmailAddress2", "EmailAddress3"))
$phoneNumberColumns = new-object System.Collections.Specialized.StringCollection
$foo = $phoneNumberColumns.AddRange(@("AssistantPhone", "BusinessFax", "BusinessPhone", "BusinessPhone2", "Callback", "CarPhone"))
$foo = $phoneNumberColumns.AddRange(@("CompanyMainPhone", "HomeFax", "HomePhone", "HomePhone2", "Isdn", "MobilePhone", "OtherFax"))
$foo = $phoneNumberColumns.AddRange(@("OtherTelephone", "Pager", "PrimaryPhone", "RadioPhone", "Telex", "TtyTddPhone"))
$physicalAddressColumns = new-object System.Collections.Specialized.StringCollection
$foo = $physicalAddressColumns.AddRange(@("Business", "Home", "Other"))

$exchService = new-object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2007_SP1)
$exchService.UseDefaultCredentials = $true
$exchService.AutodiscoverUrl($autoDiscoverAddress)
$pfsRoot = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchService, [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::publicFoldersRoot)

$tinyView = new-object Microsoft.Exchange.WebServices.Data.FolderView(2)
$displayNameProperty = [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName
$folderPathSplits = $folderPath.Split(@('\'))
$folder = $pfsRoot
for ($x = 0; $x -lt $folderPathSplits.Length;$x++)
{
$filter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo($displayNameProperty, $folderPathSplits[$x])
$results = $folder.FindFolders($filter, $tinyView)
if ($results.TotalCount -gt 1)
{
("Ambiguous name: " + $folderPathSplits[$x])
return
}
elseif ($results.TotalCount -lt 1)
{
("Folder not found: " + $folderPathSplits[$x])
return
}
$folder = $results.Folders[0]
}

$importReader = new-object System.IO.StreamReader($importFile)
$headers = $importReader.ReadLine().Split(@(','))
$line = 1

while ($null -ne ($buffer = $importReader.ReadLine()))
{
$line++
if ($buffer.Length -gt 0)
{
$newContact = new-object Microsoft.Exchange.WebServices.Data.Contact($exchService)
$columns = $buffer.Split(@(','))
for ($x = 0; $x -lt $headers.Length; $x++)
{
if ($columns[$x].Length -lt 1)
{
continue
}
if ($emailAddressColumns.Contains($headers[$x]))
{
$emailAddressType = $headers[$x]
$emailAddressType = [Microsoft.Exchange.WebServices.Data.EmailAddressKey]::$emailAddressType
$newContact.EmailAddresses[$emailAddressType] = $columns[$x]
}
elseif ($phoneNumberColumns.Contains($headers[$x]))
{
$phoneType = $headers[$x]
$phoneType = [Microsoft.Exchange.WebServices.Data.PhoneNumberKey]::$phoneType
$newContact.PhoneNumbers[$phoneType] = $columns[$x]
}
elseif ($physicalAddressColumns.Contains($headers[$x]))
{
$addressFields = $columns[$x].Split(@('%'))
$addressType = $headers[$x]
$addressType = [Microsoft.Exchange.WebServices.Data.PhysicalAddressKey]::$addressType
$address = new-object Microsoft.Exchange.WebServices.Data.PhysicalAddressEntry
$address.Street = $addressFields[0]
$address.City = $addressFields[1]
$address.State = $addressFields[2]
$address.CountryOrRegion = $addressFields[3]
$address.PostalCode = $addressFields[4]
$newContact.PhysicalAddresses[$addressType] = $address
}
elseif ($headers[$x] -eq "Birthday")
{
$newContact.Birthday = [System.DateTime]::parse($columns[$x])
}
else
{
$attribute = $headers[$x]
$newContact.$attribute = $columns[$x]
}
}
$newContact.Save($folder.Id)
}
 
You save the text to a file called Import-PFContacts.ps1. Open Exchange Management Shell, and change your Execution Policy to allow unsigned scripts:

Code:
[url=http://technet.microsoft.com/en-us/library/dd347628.aspx]Set-ExecutionPolicy[/url] unrestricted

Then run the script:
Code:
.\Import-PFContacts.ps1

How to pass variables is clearly shown in the code you posted.

Pat Richard MVP
Plan for performance, and capacity takes care of itself. Plan for capacity, and suffer poor performance.
 
Thanks for the information, I think I got the script created ok but when I go to run it, it just opens the ps1 file in notepad. Trying various parameters but nbot 100% sure what to put as location for public folders. We'd like it to go in Business public folder as shown in attached image.

Thanks again,
Joe
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top