Naimad
Naimad

Reputation: 39

Send-Mailmessage foreach in body

I have a script which gives me back a few usernames in a variable. I need now a mail with a specific text for every user (in one mail). I get the usernames "abcdef, popefp, nvmwmn"

Lets say i need the text:

Hello abcdef Hello popefp Hello nvmwmn

How do i create the foreach out of my code? I wrote $help where im lost.

   Get-ADObject -SearchBase "OU=Mailbox,OU=Global,OU=Group,OU=1,DC=test,DC=it,DC=nl" -Filter 'ObjectClass -eq "group"' | Select Name | ForEach-Object -Process {
  $username = ($_.Name -replace "G_test_Mailbox_", "") #>> "C:\temp\export-adusers.csv"
  $enabled = (Get-ADUser -Filter ('SamAccountName -eq "'+$username+'"') | Select-Object Enabled)
  if(!($enabled).Enabled){
  #if($enalbed -eq $false){
    $username  
  }
    
}

$emailbody = foreach ($help in $username){
"hello $username"}

$PSEmailServer = "smtp.wow.nl"
  
  Send-MailMessage -to "[email protected]" -From "[email protected]" -Subject "mailbox" -Body "$emailbody"

Upvotes: 0

Views: 645

Answers (2)

Naimad
Naimad

Reputation: 39

Thanks for the response i was already able to fix it.

$mail = @()

Get-ADObject -SearchBase "OU=Mailbox,OU=Global,OU=Group,OU=1,DC=test,DC=it,DC=nl" -Filter 'ObjectClass -eq "group"' | Select Name | ForEach-Object {
  $username = ($_.Name -replace "G_test_Mailbox_", "") #>> "C:\temp\export-adusers.csv"
  $mail += Get-ADUser -Filter ('SamAccountName -eq "'+$username+'"') -Properties * | Where-Object {$_.Enabled -eq $False} | Select-Object SamAccountName 
  
}


$mail.SamAccountName | ForEach-Object{
$emailbody = $emailbody + "Remove-ADGroup -Identity g_test_mailbox" + $_ + "`r`n"
}

$PSEmailServer = "smtp.wow.nl"
  
 Send-MailMessage -to "[email protected]" -From "[email protected]" -Subject "Mailbox" -Body $emailbody

Upvotes: 0

Patrick Mcvay
Patrick Mcvay

Reputation: 2281

I believe you are looking for something like below. Basically, you need to assign your usernames list to a variable. Then you will loop through that variable to make your message. There are other ways to do this, but I tried to keep it close to your current script.

  $users = Get-ADObject -SearchBase "OU=Mailbox,OU=Global,OU=Group,OU=1,DC=test,DC=it,DC=nl" -Filter 'ObjectClass -eq "group"' | Select Name | ForEach-Object -Process {
  $username = ($_.Name -replace "G_test_Mailbox_", "") #>> "C:\temp\export-adusers.csv"
  $enabledUsers = (Get-ADUser -Filter ('SamAccountName -eq "'+$username+'"') | Select-Object Enabled)
  if(!($enabled).Enabled){
  #if($enalbed -eq $false){
    $username  
  }
    
}

$emailbody = foreach ($user in $users){
"hello $user"}

$PSEmailServer = "smtp.wow.nl"
  
  Send-MailMessage -to "[email protected]" -From "[email protected]" -Subject "mailbox" -Body "$emailbody"

To avoid looping through the usernames twice, you could just use the script below.

  $emailbody = ""

  
  Get-ADObject -SearchBase "OU=Mailbox,OU=Global,OU=Group,OU=1,DC=test,DC=it,DC=nl" -Filter 'ObjectClass -eq "group"' | Select Name | ForEach-Object -Process {
  $username = ($_.Name -replace "G_test_Mailbox_", "") #>> "C:\temp\export-adusers.csv"
  $enabledUsers = (Get-ADUser -Filter ('SamAccountName -eq "'+$username+'"') | Select-Object Enabled)
  if(!($enabled).Enabled){
    $emailbody = $emailbody + "Hello $username "
  }
    
}

$PSEmailServer = "smtp.wow.nl"
  
  Send-MailMessage -to "[email protected]" -From "[email protected]" -Subject "mailbox" -Body "$emailbody"

Upvotes: 1

Related Questions