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"
}