Solved

Add Primary SMTP - PowerShell Exchange

Posted on 2014-02-24
27
447 Views
Last Modified: 2014-02-25
I am in the middle of making a PowerShell script which will add a primary SMTP Address for a list of users pulled from a CSV.

What I am trying to do is have the script ask for the primary SMTP domain for the list of users, it will read-host for the input of the domain. Then write the input to the command.

I would like to be able to use the FirstNameLastName then fill out the domain with the read-host input.

Here is an example without all the fluff... I am stuck on the First Name/Last Name part in the Email address. How would I make this auto populate in the script based on the accounts attributes.

Write-Host "Enter the Email Domain to use as the Primary SMTP Address for users:"
$Emaildomain = Read-Host
Import-Csv $inputCVS |foreach {
	$getUsername = $_.User

Set-Mailbox "$getUsername" -EmailAddress "iamstuckhere@$Emaildomain"

Open in new window

0
Comment
Question by:alexr54
[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
  • 16
  • 11
27 Comments
 
LVL 2

Author Comment

by:alexr54
ID: 39883547
Tried

Set-Mailbox "$getUsername" -EmailAddresses "SMTP:$($_.FirstName)($_.LastName)@$EmailDomain"

With no luck
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39883563
is your $_.User = to samaccountname or the display name of the mailbox?

you can add $User = get-aduser -id $getusername

$emailaddress = $user.givenname + $user.surname + $emaildomain
set-mailbox $getusername -emailadress $emailaddress
0
 
LVL 2

Author Comment

by:alexr54
ID: 39883606
is your $_.User = to samaccountname or the display name of the mailbox?

It is pulling the Display Name from the CSV.

I will try what your suggesting, but not sure it will work.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 2

Author Comment

by:alexr54
ID: 39883616
$user.givenname + $user.surname

Its not pulling anything.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39883623
import-module ActiveDirectory.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39883635
Write-Host "Enter the Email Domain to use as the Primary SMTP Address for users:"
$Emaildomain = Read-Host
$UserNames = Import-Csv $inputCVS
Import-Module ActiveDirectory
Foreach ($username in $usernames)
{
$user = get-aduser -id $username
$emailaddress = $user.givenname + $user.surname + $emaildomain

Set-Mailbox $user -EmailAddress $emailaddres
}
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39883642
first make sure
$usernames comes up with the list of users

then check if $user return the correct result
$emailaddress to see if the email display correct

before set-mailbox on your script

test if it is working on this

Write-Host "Enter the Email Domain to use as the Primary SMTP Address for users:"
$Emaildomain = Read-Host
$UserNames = Import-Csv $inputCVS
$UserNames
Import-Module ActiveDirectory
Foreach ($username in $usernames)
{
$user = get-aduser -id $username
$user
$emailaddress = $user.givenname + $user.surname + $emaildomain
$emailaddress

#Set-Mailbox $user -EmailAddress $emailaddres
}

Please also ensure you have the path of the CVS $inputCVS=the path
0
 
LVL 2

Author Comment

by:alexr54
ID: 39883956
Here is my full script. I cant manage to get what you suggested to work. I cant get the $names to pull data from AD.

#--------------------------------------------------------------------------------------
$PackageName = "Update_SMTP"
$logDir = "c:\Windows\Temp\"
$inputCVS = "c:\Windows\Temp\Users.csv"
$logFile = "$logDir\Update_SMTP_Log.txt"

#Colors
$colorMessage = "white"
$colorStatusGood = "green"
$colorStatusBad = "red"
$colorBG = "black"
$colorHost = "yellow"
$colorExit = "cyan"
#--------------------------------------------------------------------------------------


Function writeLog($logText, $statusText, $colorStatus) {
	Add-Content $logFile -Value "$logText" -Force
	Write-Host "Host Message : " -ForegroundColor $colorHost -BackgroundColor $colorBG -NoNewline; Write-Host $statusText -ForegroundColor $colorStatus -BackgroundColor $colorBG
}

Import-Module ActiveDirectory

Write-Host "Enter the Email Domain to use as the Primary SMTP Address for users: " -ForegroundColor Magenta
$Emaildomain = Read-Host

$names = $user.givenname + $user.surname 




$logDate = Get-Date; Set-Content $logFile -Value "$PackageName Script Started - $logDate" -Force

#Import CSV File
$logDate = Get-Date; writeLog "Importing CSV file and looping through file - $logDate" "Importing CSV file..." $colorMessage
Import-Csv $inputCVS |foreach {
	$getUsername = $_.User




Try {
		$logDate = Get-Date; writeLog "     Processing...$getUsername - $logDate" "     Processing...$getUsername..." $colorMessage
        
        
        
	     Set-Mailbox "$getUsername" -PrimarySMTPAddress "$names@$emaildomain"
        
	}



	Catch {
	  $logDate = Get-Date; writeLog "     Error Processing...$getUsername - $logDate" "     Error Processing...$getUsername..." $colorStatusBad
	  $logDate = Get-Date; writeLog "         Error: $_ - $logDate" "          Error: Writing to Log File..." $colorStatusBad
	}
	
	$logDate = Get-Date; writeLog "     Processed...$getUsername - $logDate" "     Processed...$getUsername..." $colorMessage
	Start-Sleep 3
}

Open in new window

0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39883995
"$names@$emaildomain" this will not work

you can do $emailaddress = $Names + "@" + $emaildomain
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39884004
$names = $user.givenname + $user.surname  

this will not work as well, because you have to do it after "foreach"
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39884015
#--------------------------------------------------------------------------------------
$PackageName = "Update_SMTP"
$logDir = "c:\Windows\Temp\"
$inputCVS = "c:\Windows\Temp\Users.csv"
$logFile = "$logDir\Update_SMTP_Log.txt"

#Colors
$colorMessage = "white"
$colorStatusGood = "green"
$colorStatusBad = "red"
$colorBG = "black"
$colorHost = "yellow"
$colorExit = "cyan"
#--------------------------------------------------------------------------------------


Function writeLog($logText, $statusText, $colorStatus) {
      Add-Content $logFile -Value "$logText" -Force
      Write-Host "Host Message : " -ForegroundColor $colorHost -BackgroundColor $colorBG -NoNewline; Write-Host $statusText -ForegroundColor $colorStatus -BackgroundColor $colorBG
}

Import-Module ActiveDirectory

Write-Host "Enter the Email Domain to use as the Primary SMTP Address for users: " -ForegroundColor Magenta
$Emaildomain = Read-Host






$logDate = Get-Date; Set-Content $logFile -Value "$PackageName Script Started - $logDate" -Force

#Import CSV File
$logDate = Get-Date; writeLog "Importing CSV file and looping through file - $logDate" "Importing CSV file..." $colorMessage
$Usernames = Import-Csv $inputCVS
foreach ($username in $usernames)
{
$getUsername = $_.User
$Userid = get-aduser -id $getUsername
$name = $Userid.givenname + $Userid.surname
$emailaddress = $name + "@" + $Emaildomain


Try {
            $logDate = Get-Date; writeLog "     Processing...$getUsername - $logDate" "     Processing...$getUsername..." $colorMessage
       
       
       
           Set-Mailbox "$getUsername" -PrimarySMTPAddress $emailaddress
       
      }



      Catch {
        $logDate = Get-Date; writeLog "     Error Processing...$getUsername - $logDate" "     Error Processing...$getUsername..." $colorStatusBad
        $logDate = Get-Date; writeLog "         Error: $_ - $logDate" "          Error: Writing to Log File..." $colorStatusBad
      }
      
      $logDate = Get-Date; writeLog "     Processed...$getUsername - $logDate" "     Processed...$getUsername..." $colorMessage
      Start-Sleep 3
}
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:



Select all

Open in new window
0
 
LVL 2

Author Comment

by:alexr54
ID: 39884254
Thanks for the quick replies.
I knew "$names@$emaildomain" would not work, I just put it in there so it would be clear of how the format should look like.

I will test this out tomorrow when I get into the office.

Appreciate the help.
0
 
LVL 2

Author Comment

by:alexr54
ID: 39884262
As a quick follow up, another thing I was trying to do but did not get it to work yet is improve the logging. Right now it will write any errors only.

How would I be able to add to the log all actions, for example it should read if no action was taken and if the setting applied. So far it only writes errors and if there is no error, it writes nothing of value, just that it succeeded.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39884719
added after the set-mailbox

if ($error.count -eq 0)
{
$logDate = Get-Date; writeLog "     Processed...$getUsername - $logDate" "     Processed...$getUsername..." $colorMessage
}

so if total error = 0, it then write to the log.
0
 
LVL 2

Author Comment

by:alexr54
ID: 39885574
This is the error I get now when I tried your updated script.

I am getting this error now
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39885611
$inputCVS = "c:\Windows\Temp\Users.csv"
$Usernames = Import-Csv $inputCVS

$Usernames

what result do you get with just this 3 lines?

it is not translate the result to the $getusername, the identity is a null result......

and the try

$usernames.headerofyourcvs
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39885681
$getUsername = $Username
0
 
LVL 2

Author Comment

by:alexr54
ID: 39885715
Here is the result I get with just those 3 lines.resultHere is the result I get with just those 3 lines.

I apologize, but I don't feel comfortable making my company name public info.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39885722
can you post the sample of the csv file?
0
 
LVL 2

Author Comment

by:alexr54
ID: 39885761
User
o365test4@testdomain.com


This is the User Principal Name and not necessarily the correct SMTP address. Which is why I would like to pull from AD the FirstName and LastName.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39885779
#--------------------------------------------------------------------------------------
$PackageName = "Update_SMTP"
$logDir = "c:\Windows\Temp\"
$inputCVS = "c:\Windows\Temp\Users.csv"
$logFile = "$logDir\Update_SMTP_Log.txt"

#Colors
$colorMessage = "white"
$colorStatusGood = "green"
$colorStatusBad = "red"
$colorBG = "black"
$colorHost = "yellow"
$colorExit = "cyan"
#--------------------------------------------------------------------------------------


Function writeLog($logText, $statusText, $colorStatus) {
      Add-Content $logFile -Value "$logText" -Force
      Write-Host "Host Message : " -ForegroundColor $colorHost -BackgroundColor $colorBG -NoNewline; Write-Host $statusText -ForegroundColor $colorStatus -BackgroundColor $colorBG
}

Import-Module ActiveDirectory

Write-Host "Enter the Email Domain to use as the Primary SMTP Address for users: " -ForegroundColor Magenta
$Emaildomain = Read-Host






$logDate = Get-Date; Set-Content $logFile -Value "$PackageName Script Started - $logDate" -Force

#Import CSV File
$logDate = Get-Date; writeLog "Importing CSV file and looping through file - $logDate" "Importing CSV file..." $colorMessage
$Usernames = Import-Csv $inputCVS
foreach ($username in $usernames)
{
$Userid = Get-ADUser -Filter * | ? {$_.UserPrincipalName -eq $username}
$name = $Userid.givenname + $Userid.surname
$getUsername = $Userid.samaccountname
$emailaddress = $name + "@" + $Emaildomain


Try {
            $logDate = Get-Date; writeLog "     Processing...$getUsername - $logDate" "     Processing...$getUsername..." $colorMessage
       
       
       
           Set-Mailbox "$getUsername" -PrimarySMTPAddress $emailaddress
       
      }



      Catch {
        $logDate = Get-Date; writeLog "     Error Processing...$getUsername - $logDate" "     Error Processing...$getUsername..." $colorStatusBad
        $logDate = Get-Date; writeLog "         Error: $_ - $logDate" "          Error: Writing to Log File..." $colorStatusBad
      }
     
      $logDate = Get-Date; writeLog "     Processed...$getUsername - $logDate" "     Processed...$getUsername..." $colorMessage
      Start-Sleep 3
}
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39885785
$inputCVS = "c:\Windows\Temp\Users.csv"
$Usernames = Import-Csv $inputCVS

foreach ($username in $usernames)
{
$Userid = Get-ADUser -Filter * | ? {$_.UserPrincipalName -eq $username}
$name = $Userid.givenname + $Userid.surname
$getUsername = $Userid.samaccountname
$emailaddress = $name + "@" + $Emaildomain

$Userid
$name
$getusername
$emailaddress

test with this line and see the result comes up correctly.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39885789
if it doesn't work you might need to change $username to $username.user since the header in your cvs is user

$inputCVS = "c:\Windows\Temp\Users.csv"
$Usernames = Import-Csv $inputCVS

foreach ($username in $usernames)
{
$Userid = Get-ADUser -Filter * | ? {$_.UserPrincipalName -eq $username.user}
$name = $Userid.givenname + $Userid.surname
$getUsername = $Userid.samaccountname
$emailaddress = $name + "@" + $Emaildomain

$Userid
$name
$getusername
$emailaddress
0
 
LVL 14

Accepted Solution

by:
Justin Yeung earned 500 total points
ID: 39885809
tested add $username.user on the search under the line of $_.UserPrincipalName -eq $username


so it is  $_.UserPrincipalName -eq $username.user
0
 
LVL 2

Author Comment

by:alexr54
ID: 39886032
Ok so it works only if the users UPN is used in the CSV, if its the users Display Name, it will not pull the First Name and Last Name from AD.

Either way.. This does work! And its great!! Thanks so much for your help with this!!
0
 
LVL 2

Author Closing Comment

by:alexr54
ID: 39886035
This made it work.
0
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39886046
if your csv is displayname, you have change the search field to

? {$_.Name -eq $username.user}
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

632 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