Since you only reference $user.AppOwner you can change the import to this.
$Users = Import-Csv -path "G:\Users.csv" | Select-Object -Property AppOwner -Unique
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I am using powershell script that reads input csv, and sends email notifications to email address mentioned in email column. As email address mentioned in each row is same, script should send only 1 email. As in my current script I am using foreach loop, it is sending email notification for each row in csv. For example, if csv has 5 rows, and same email address is mentioned in all 5 rows, it is sending 5 email notifications. How do I modify for loop, so that script only reads 1 row and sends email notification only once. Attaching my current script
$Users = Import-Csv -path "G:\Users.csv"
foreach($user in $users) {
$bodytxt = @"
Hello,
Email Body.
"@
$EmailFrom = 'xyz@outlook.com'
$EmailTo = $user.AppOwner
$EmailSubject = "Review Notification"
$EmailBody = "$bodytxt"
$SMTPServer = 'smtp-relay.com'
$EncryptedPasswordFile = "$home\Documents\PSCredential\MailJet.txt"
$username="abc@outlook.com"
$password = Get-Content -Path $EncryptedPasswordFile | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PSCredential($username, $password)
Send-MailMessage -ErrorAction Stop -from "$EmailFrom" -to "$EmailTo" -subject "$EmailSubject" -body "$EmailBody" -SmtpServer "$SMTPserver" -Priority "Normal" -Credential $credential -Port 587 -UseSsl
}
Since you only reference $user.AppOwner you can change the import to this.
$Users = Import-Csv -path "G:\Users.csv" | Select-Object -Property AppOwner -Unique
Or this:
$User = Import-Csv -path "G:\Users.csv" | Select-Object -Expand AppOwner -First 1
Then change line #6 to $EmailTo = $User