Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 466
  • Last Modified:

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

0
alexr54
Asked:
alexr54
  • 16
  • 11
1 Solution
 
alexr54Author Commented:
Tried

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

With no luck
0
 
Justin YeungCommented:
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
 
alexr54Author Commented:
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
alexr54Author Commented:
$user.givenname + $user.surname

Its not pulling anything.
0
 
Justin YeungCommented:
import-module ActiveDirectory.
0
 
Justin YeungCommented:
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
 
Justin YeungCommented:
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
 
alexr54Author Commented:
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
 
Justin YeungCommented:
"$names@$emaildomain" this will not work

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

this will not work as well, because you have to do it after "foreach"
0
 
Justin YeungCommented:
#--------------------------------------------------------------------------------------
$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
 
alexr54Author Commented:
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
 
alexr54Author Commented:
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
 
Justin YeungCommented:
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
 
alexr54Author Commented:
This is the error I get now when I tried your updated script.

I am getting this error now
0
 
Justin YeungCommented:
$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
 
Justin YeungCommented:
$getUsername = $Username
0
 
alexr54Author Commented:
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
 
Justin YeungCommented:
can you post the sample of the csv file?
0
 
alexr54Author Commented:
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
 
Justin YeungCommented:
#--------------------------------------------------------------------------------------
$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
 
Justin YeungCommented:
$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
 
Justin YeungCommented:
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
 
Justin YeungCommented:
tested add $username.user on the search under the line of $_.UserPrincipalName -eq $username


so it is  $_.UserPrincipalName -eq $username.user
0
 
alexr54Author Commented:
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
 
alexr54Author Commented:
This made it work.
0
 
Justin YeungCommented:
if your csv is displayname, you have change the search field to

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

Featured Post

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

  • 16
  • 11
Tackle projects and never again get stuck behind a technical roadblock.
Join Now