PowerShell Array Send Email

I have a script that updates a users name based off of a csv file. I would like to send the users a email with there new login after the script has run. Currently, the script only sends to the first user instead of the entire list. Do you have any ideal on how I can get the script to send to a email to each users with their information? 

$array = @(Import-CSV "D:\Scripts\AH\NameChange2\ExportCSV\NameChange.csv") 
foreach($value in $array)
{

$CurrentEmail += $value.CurrentEmailAddress 
$NewEmail1 = $value.NewEmailAddress
$NewEmail2 = $value.NewFirstName + "." + $value.Title + "." + $value.NewLastName + $EmailCengage

    $User1= Get-ADUser -id  ($value.NewFirstName.substring(0,1) + $value.NewLastName)
    $User2= Get-ADUser -id  ($value.NewFirstName.substring(0,2) + $value.NewLastName)
    $User3= Get-ADUser -id  ($value.NewFirstName.substring(0,3) + $value.NewLastName)
    
        $userUPN= Get-ADUser -filter {Emailaddress -eq $NewEmail1}
        $userUPN= Get-ADUser -filter {Emailaddress -eq $NewEmail2}
    


if($User1 -eq $null -and $userUPN -eq $null){

$NewLogin1 = $NewEmail1
set-RemoteMailbox -Id $value.EnterPCLogin.split('\')[-1] -PrimarySmtpAddress $value.NewEmailAddress
    
    Set-ADUser -Id $value.EnterPCLogin.split('\')[-1] -GivenName $value.NewFirstName -Surname $value.NewLastName -UserPrincipalName $value.NewEmailAddress -SamAccountName ($value.NewFirstName.substring(0,1)+$value.NewLastName) -DisplayName ($value.NewLastName +  ", " + $value.NewFirstName) -Emailaddress $value.NewEmailAddress
    #-Add @{proxyAddresses = "smtp:" + $value.NewEmailAddress

$Where = [array]::IndexOf($CurrentEmail, $NewEmail1)
    Write-Host "Customer Name: " $CurrentEmail[$Where]

$emailFrom = "DoNotReply@cage.com"
$emailTo = "aaron.harris@cage.com , $CurrentEmail"

$subject = "Name Change Request $NewLogin1, $NewLogin2"
$body = "Your New Login Name is  $NewLogin1, $NewLogin2" 
$body += $a.sum | Out-String
$smtpServer = "mailhost.learning.com" # Replace with your SMTP Server IP
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)


}

Elseif($User1 -ne $null -and $User2 -eq $null -and $userUPN2 -eq $null){
$NewLogin2 = $NewEmail2
set-RemoteMailbox -Id $value.EnterPCLogin.split('\')[-1] -PrimarySmtpAddress $NewEmail2 
    

Set-ADUser -Id $value.EnterPCLogin.split('\')[-1]  -GivenName $value.NewFirstName -Surname $value.NewLastName -UserPrincipalName ($value.NewFirstName+"."+$value.Title + "."+$value.NewLastName+$EmailCengage) -SamAccountName ($value.NewFirstName.substring(0,2)+$value.NewLastName)  -DisplayName ($value.NewLastName + ", " + $value.Title + $value.NewFirstName) -Emailaddress $value.EmailAddress 
#-Add @{proxyAddresses =  "smtp:"+ ($value.NewFirstName + "." + $value.Title + "." + $value.NewLastName + $Emailengage)
#

$Where = [array]::IndexOf($CurrentEmail, $NewEmail1)
    Write-Host "Customer Name: " $CurrentEmail[$Where]

$emailFrom = "DoNotReply@.com"
$emailTo = "aaron.harris@.com , $CurrentEmail"

$subject = "Name Change Request $NewLogin1, $NewLogin2"
$body = "Your New Login Name is  $NewLogin1, $NewLogin2" 
$body += $a.sum | Out-String
$smtpServer = "mailhost.earning.com" # Replace with your SMTP Server IP
$smtp = new-object Net.xxx.SmtpClient($smtpServer)
$smtp.Send($emailFrom, $emailTo, $subject, $body)

}






write-host "First Name =" $value.NewFirstName "Last Name =" $value.NewLastName "Middle Name=" $value.Title "Login =" $value.EnterPCLogin.split('\')[-1]

Write-Host "final result"
Write-Host "user1 $User1 "
Write-Host "user2 $User2 "
Write-Host "user3 $User3 "
Write-Host "UPN1 $UserUPN1"
Write-Host "UPN1 $UserUPN2"

}

July 28th, 2015 9:59am

The script already does what you ask:

$emailTo = "aaron.harris@cage.com , $CurrentEmail"

Perhaps you should aske the author to look at this for you. When you send the mail ther will not likely be a mailbox because you have just created the account.  THe mailbox will not be immediately provisioned.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 10:19am

This is for an existing mail account. The script will change the users last name and email address. So, the account is available to receive mail. Also, I'm sending the email to the users old email address which will still be listed as the alias. 
July 28th, 2015 11:23am

What is stopping you from using the correct email?
Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 12:27pm

why this? 

$array = @(Import-CSV "D:\Scripts\AH\NameChange2\ExportCSV\NameChange.csv") 
foreach($value in $array)
{

$CurrentEmail += $value.CurrentEmailAddress 

July 28th, 2015 1:01pm

why this? 

$array = @(Import-CSV "D:\Scripts\AH\NameChange2\ExportCSV\NameChange.csv") 
foreach($value in $array)
{

$CurrentEmail += $value.CurrentEmailAddress 

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 1:03pm

why this? 

$array = @(Import-CSV "D:\Scripts\AH\NameChange2\ExportCSV\NameChange.csv") 
foreach($value in $array)
{

$CurrentEmail += $value.CurrentEmailAddress 

July 28th, 2015 1:13pm

But, as noted, that email is no longer valid.  Perhaps the domain no longer exists.  No mater where and how you add it it will not receive mail.  The OP needs to use the new mail address.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 1:23pm

The email is sent to the users old address, which is listed as an alias. So the address is valid. The name change is adding an additional address but not removing the old address. 
July 28th, 2015 1:31pm

What is the purpose of the way it's written as opposed to...

$array = Import-CSV "NameChange.csv"
foreach ($value in $array) {

$CurrentEmail = $value.CurrentEmailAddress

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 1:32pm

The email is sent to the users old address, which is listed as an alias. So the address is valid. The name change is adding an additional address but not removing the old addr
July 28th, 2015 1:49pm

Looking closely at the script is has many logic errors that you need to fix before addressing whether it will send mail.

I cannot determine what you are trying to do from the logic. The logic is redundant or incorrect or both.

You also need to simplify and format the script to be more readable.

Why are you trying to get so many user objects.  Do the users have more than one account? Two accounts cannot have the same email in the same Exchange organization. 

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 1:58pm

The issue is the email is not sent to all users listed in the csv file. The email is only sent to the first users. However, all other aspects of the script work as intend. 
July 28th, 2015 2:00pm

This resolved my issue. Thank you!!!!!!!!!!!
Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:05pm

Looking closely at the script is has many logic errors that you need to fix before addressing whether it will send mail.

You also need to simplify and format the script to be more readable.

July 28th, 2015 2:05pm

What first users.  The email address is set to the mail address in each line of the CSV.

This makes no sens:

 $User1 = Get-ADUser -id  ($value.NewFirstName.substring(0, 1) + $value.NewLastName)
 $User2 = Get-ADUser -id  ($value.NewFirstName.substring(0, 2) + $value.NewLastName)
 $User3 = Get-ADUser -id  ($value.NewFirstName.substring(0, 3) + $value.NewLastName)
 
 $userUPN = Get-ADUser -filter { Emailaddress -eq $NewEmail1 }
 $userUPN = Get-ADUser -filter { Emailaddress -eq $NewEmail2 }
 
 And it will likely not work.  This could be the source of you whole proble,

Why are you doing this?  If you are trying to guess at the user ID then nothing is predictable.  You must use either the SamAccountName or DistinguishedName to identify a unique user.

Can you contact the author of this to get help.  I suspect the original may have worked but someone tried to change it and has mad it non-functional.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:06pm

No need to be rude jrv. Dan was able to determine the issue.  
July 28th, 2015 2:07pm

Here is a better formatted version so you can try and follow what is happening:

$array = @(Import-CSV "D:\Scripts\AH\NameChange2\ExportCSV\NameChange.csv")
foreach ($value in $array) {
	
	$CurrentEmail += $value.CurrentEmailAddress
	$NewEmail1 = $value.NewEmailAddress
	$NewEmail2 = $value.NewFirstName + "." + $value.Title + "." + $value.NewLastName + $EmailCengage
	
	$User1 = Get-ADUser -id  ($value.NewFirstName.substring(0, 1) + $value.NewLastName)
	$User2 = Get-ADUser -id  ($value.NewFirstName.substring(0, 2) + $value.NewLastName)
	$User3 = Get-ADUser -id  ($value.NewFirstName.substring(0, 3) + $value.NewLastName)
	
	$userUPN = Get-ADUser -filter { Emailaddress -eq $NewEmail1 }
	$userUPN = Get-ADUser -filter { Emailaddress -eq $NewEmail2 }
	
	
	
	if ($User1 -eq $null -and $userUPN -eq $null) {
		
		$NewLogin1 = $NewEmail1
		set-RemoteMailbox -Id $value.EnterPCLogin.split('\')[-1] -PrimarySmtpAddress $value.NewEmailAddress
		
		Set-ADUser -Id $value.EnterPCLogin.split('\')[-1] -GivenName $value.NewFirstName -Surname $value.NewLastName -UserPrincipalName $value.NewEmailAddress -SamAccountName ($value.NewFirstName.substring(0, 1) + $value.NewLastName) -DisplayName ($value.NewLastName + ", " + $value.NewFirstName) -Emailaddress $value.NewEmailAddress
		#-Add @{proxyAddresses = "smtp:" + $value.NewEmailAddress
		
		$Where = [array]::IndexOf($CurrentEmail, $NewEmail1)
		Write-Host "Customer Name: " $CurrentEmail[$Where]
		
		$emailFrom = "DoNotReply@cage.com"
		$emailTo = "aaron.harris@cage.com , $CurrentEmail"
		
		$subject = "Name Change Request $NewLogin1, $NewLogin2"
		$body = "Your New Login Name is  $NewLogin1, $NewLogin2"
		$body += $a.sum | Out-String
		$smtpServer = "mailhost.learning.com" # Replace with your SMTP Server IP
		$smtp = new-object Net.Mail.SmtpClient($smtpServer)
		$smtp.Send($emailFrom, $emailTo, $subject, $body)
		
		
	} Elseif ($User1 -ne $null -and $User2 -eq $null -and $userUPN2 -eq $null) {
		$NewLogin2 = $NewEmail2
		set-RemoteMailbox -Id $value.EnterPCLogin.split('\')[-1] -PrimarySmtpAddress $NewEmail2
		
		
		Set-ADUser -Id $value.EnterPCLogin.split('\')[-1]  -GivenName $value.NewFirstName -Surname $value.NewLastName -UserPrincipalName ($value.NewFirstName + "." + $value.Title + "." + $value.NewLastName + $EmailCengage) -SamAccountName ($value.NewFirstName.substring(0, 2) + $value.NewLastName)  -DisplayName ($value.NewLastName + ", " + $value.Title + $value.NewFirstName) -Emailaddress $value.EmailAddress
		#-Add @{proxyAddresses =  "smtp:"+ ($value.NewFirstName + "." + $value.Title + "." + $value.NewLastName + $Emailengage)
		#
		
		$Where = [array]::IndexOf($CurrentEmail, $NewEmail1)
		Write-Host "Customer Name: " $CurrentEmail[$Where]
		
		$emailFrom = "DoNotReply@.com"
		$emailTo = "aaron.harris@.com , $CurrentEmail"
		
		$subject = "Name Change Request $NewLogin1, $NewLogin2"
		$body = "Your New Login Name is  $NewLogin1, $NewLogin2"
		$body += $a.sum | Out-String
		$smtpServer = "mailhost.earning.com" # Replace with your SMTP Server IP
		$smtp = new-object Net.xxx.SmtpClient($smtpServer)
		$smtp.Send($emailFrom, $emailTo, $subject, $body)
		
	}
		
	write-host "First Name =" $value.NewFirstName "Last Name =" $value.NewLastName "Middle Name=" $value.Title "Login =" $value.EnterPCLogin.split('\')[-1]
	
	Write-Host "final result"
	Write-Host "user1 $User1 "
	Write-Host "user2 $User2 "
	Write-Host "user3 $User3 "
	Write-Host "UPN1 $UserUPN1"
	Write-Host "UPN1 $UserUPN2"
	
}

As you can see there is only one path that sends email.

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:09pm

The following wil not get you a uniquely identifiable user.

$User1 = Get-ADUser -id  ($value.NewFirstName.substring(0, 1) +$value.NewLastName)
$User2
= Get-ADUser -id  ($value.NewFirstName.substring(0, 2) +$value.NewLastName)
$User3
= Get-ADUser -id  ($value.NewFirstName.substring(0, 3) +$value.NewLastName)$userUPN = Get-ADUser -filter { Emailaddress -eq $NewEmail1 }
$userUPN
= Get-ADUser -filter { Emailaddress -eq $NewEmail2 }
 

July 28th, 2015 2:10pm

Your wrong jrv.... What I have will work when updating the script based on Dave's response. I was able to test and confirm. Have a happy Tuesday jrv. 
Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:12pm

What is the purpose of the way it's written as opposed to...

$array = Import-CSV "NameChange.csv"
foreach ($value in $array) {

$CurrentEmail = $value.CurrentEmailAddres
July 28th, 2015 2:13pm

Your wrong jrv.... What I have will work when updating the script based on Dave's response. I was able to test and confirm. Have a happy Tuesday
Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:16pm

jrv only one path should send mail!!!!!!!!!! This is by design!!!!!!!!!!!!!! The second path is only used if the users name is already in use. Your killing me.. You seem to be the only one that couldn't figure that at. 
July 28th, 2015 2:20pm

Depending on your organization you might be better off with using anr to find proxy addresses. I've seen many inconsistencies between proxyaddresses,mail,emailaddress attributes. 

Get-ADUser -Filter {anr -eq 'smtp:someone@domain.com'}

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:25pm

Thank you for the suggestion, I will try that as well. 
July 28th, 2015 2:28pm

Depending on your organization you might be better off with using anr to find proxy addresses. I've seen many inconsistencies between proxyaddresses,mail,emailaddress attributes. 

Get-ADUser -Filter {anr -eq 'smtp:someone@domain.com'}

Free Windows Admin Tool Kit Click here and download it now
July 28th, 2015 2:33pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics