Link to home
Start Free TrialLog in
Avatar of alexr54
alexr54Flag for United States of America

asked on

Add Primary SMTP - PowerShell Exchange

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

Avatar of alexr54
alexr54
Flag of United States of America image

ASKER

Tried

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

With no luck
Avatar of Justin Yeung
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
Avatar of alexr54

ASKER

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.
Avatar of alexr54

ASKER

$user.givenname + $user.surname

Its not pulling anything.
import-module ActiveDirectory.
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
}
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
Avatar of alexr54

ASKER

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

"$names@$emaildomain" this will not work

you can do $emailaddress = $Names + "@" + $emaildomain
$names = $user.givenname + $user.surname  

this will not work as well, because you have to do it after "foreach"
#--------------------------------------------------------------------------------------
$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
Avatar of alexr54

ASKER

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.
Avatar of alexr54

ASKER

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.
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.
Avatar of alexr54

ASKER

This is the error I get now when I tried your updated script.

User generated image
$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
$getUsername = $Username
Avatar of alexr54

ASKER

Here is the result I get with just those 3 lines.User generated imageHere is the result I get with just those 3 lines.

I apologize, but I don't feel comfortable making my company name public info.
can you post the sample of the csv file?
Avatar of alexr54

ASKER

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.
#--------------------------------------------------------------------------------------
$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
}
$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.
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
ASKER CERTIFIED SOLUTION
Avatar of Justin Yeung
Justin Yeung
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alexr54

ASKER

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!!
Avatar of alexr54

ASKER

This made it work.
if your csv is displayname, you have change the search field to

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