Advice on how methods to store data before sending mail

Hi all,

I am still getting to grips with PowerShell and still learning my way around the various options in PowerShell. 

I am working on a script that search for expired accounts in AD and there will be some where my script will take an action, for example if an expired AD user account has not been disabled my script will disable it. Then further down it will check to see if there are more AD groups than Domain Users and remove it there are.

What i would like to know is how do i collate the info from each "action" and and the end of my script if i want to send 1 large email to the IT helpdesk informing of all changes instead of an email per action? I am guessing a need to add things into an array or something and then in my Send-MailMessage command when i define the -Body that i can use a variable like $actionResults that contain all the actions?

If you can guide me to where i need to go search i would really appreciate it. Looking for various options and methods so i can work out which one is better in my situation.

Best regards

Ronnie

July 21st, 2015 3:55pm

If an account is set to expire it will automatically be disabled.

I recommend that you start by  getting the basics of PowerShell worked out.  You will have a hard time building any complex script without the basics.

You can start here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

There are many basic videos on YouTube that cover most f the fundamentals in small bites.

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:08pm

Thank you very much for your reply, i have been watching videos from CBT Nuggets website but i guess i have not yet quite worked out how to accomplish my goal.

Thank you though for the reply.

July 21st, 2015 4:22pm

"Nuggets" are only useful after you have learned the basics.  Before you learn they can be very misleading.

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:40pm

A colleague of mine helped me out in the end. We ending up adding to a string and when sending the email we used that string as part of the body in the mail sent

We used this:
$strActionsTaken += "<tr><td>" + $leaver.Name +" ("+ $leaver.SamAccountName+") Disabled</td></tr>"

August 4th, 2015 3:29am

A colleague of mine helped me out in the end. We ending up adding to a string and when sending the email we used that string as part of the body in the mail sent

We used this:
$strActionsTaken += "<tr><td>" + $leaver.Name +" ("+ $leaver.SamAccountName+") Disabled</td></tr>"

This is not helpful.  What you have marked as an answer answers nothing.  It is just a claim that you were wrong to begin with..
Free Windows Admin Tool Kit Click here and download it now
August 4th, 2015 3:33am

I am sorry you see it that way, but I thought the same with your answer. telling people to go learn the basics when all they asked was to be pointed in the right direction seems unhelpful to me.

Although I do agree that people should learn the basics before starting any complex scripting but still I felt not very not helped or guided by your answer.

If it makes it any better here is a snippet from the script.

$strActionsTaken = "" # Get all account expired AD user accounts in the EUECS domain.
$leavers = Search-ADAccount -AccountExpired -UsersOnly -SearchBase $searchBase -searchScope subtree -Server $domaincont # Running through all account expired AD user accounts.
Foreach ($leaver in $leavers) {     # Disable account if account is not disabled already.
    if ($leaver.Enabled -eq "true") {
        Disable-ADAccount -Identity $leaver.SamAccountName #-WhatIf
        $strActionsTaken += "<tr><td>" + $leaver.Name +" ("+ $leaver.SamAccountName+") Disabled</td></tr>"
    }

    # Hide from Exchange address list.
    $leaveracc = Get-ADUser -Identity $leaver.SamAccountName -Properties Name,SamAccountName,EmailAddress,msExchHideFromAddressLists
    if ($leaver.EmailAddress -like "*@*" -AND $leaver.msExchHideFromAddressLists -ne "True") {
        Set-ADUser -Identity $leaver.SamAccountName -Add @{msExchHideFromAddressLists=$TRUE} #-WhatIf
        $strActionsTaken += "<tr><td>" + $leaver.Name +" ("+ $leaver.SamAccountName+") Hidden from GAL</td></tr>"
    }

.........

# Email sending part looks like this

$expirationSummaryEmailFirstPart = "
<html><body>
<p style='font-family:Verdana; font-size:10pt'>
<b>Summary of all actions taken from last night actions taken script run:</b>
<br/><br/>
<table>
<tr><td>text of something</td></tr>
"
   
$expirationSummaryEmailSecondPart = "
</table></p></body></html>
" $htmlSummaryMailBody = $expirationSummaryEmailFirstPart + $strActionsTaken + $expirationSummaryEmailSecondPart Send-MailMessage -Body $htmlSummaryMailBody -BodyAsHtml -Subject "Daily Report - PS scheduled job Actions Taken" -From $mailFrom -To $mailTo -SmtpServer $smtpserver

August 4th, 2015 3:45am

If an account is set to expire it will automatically be disabled.

I recommend that you start by  getting the basics of PowerShell worked out.  You will have a hard time building any complex script without the basics.

You can start here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

There are many basic videos on YouTube that cover most f the fundamentals in small

Free Windows Admin Tool Kit Click here and download it now
August 4th, 2015 3:57am

Thank you for your feedback.
August 4th, 2015 4:41am

A colleague of mine helped me out in the end. We ending up adding to a string and when sending the email we used that string as part of the body in the mail sent

We used this:
$strActionsTaken += "<tr><td>" + $leaver.Name +" ("+ $leaver.SamAccountName+") Disabled</td></tr>"

Free Windows Admin Tool Kit Click here and download it now
August 4th, 2015 7:27am

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

Other recent topics Other recent topics