Send output of "Search-ADAccount" as body variable in SEND-MailMessage

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Rocky Cabral Rocky Cabral 6 months, 4 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #35587
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    Hello,

    I have a script that searches the user accounts that have not been logged on to in 30 days and email the results to a group.

    The script works except that I'm using the OUT-STRING to a $BODY variable for the SEND-MailMessage and that sends it as a long string rather than a column list.

    How can I send it as column list?

    Script:
    #Find users that have not logged on in 30 days
    $a = Search-ADAccount -AccountInactive -UsersOnly -TimeSpan 30.00:00:00 -SearchBase "OU=Accounts,DC=HQ,DC=Company,DC=Com" | ? {$_.DistinguishedName -notlike "*OU=Services,OU=Accounts,*" -and $_.DistinguishedName -notlike "*OU=Shared,OU=Accounts,*" -and $_.DistinguishedName -notlike "*OU=Inactive,OU=Accounts,*" -and $_.DistinguishedName -notlike "*OU=Special,OU=Accounts,*"}

    $b = $a | select Name

    #Variables
    $from = "IT Dept. "
    $emailaddress = "[email protected]"
    $body = Write-Output $b | Out-String
    $smtpserver = "mail"

    #Send Email Message
    Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body $body -BodyAsHtml -priority High

    #35720
    Profile photo of Wei-Yen Tan
    Wei-Yen Tan
    Participant

    Look at Convertto-HTML as a way to do this

    $html = Convertto-Html -Body "$a"
    
    $body = $html | out-string
    
    Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body $body -BodyAsHtml -priority High
    
    
    #36110
    Profile photo of Rocky Cabral
    Rocky Cabral
    Participant

    Thank you Wei-Yen. I'll try that out.

    I actually found another way but the OU I'm referencing has sub-OUs that contain service or shared accounts that where created in Office 365 for a special purpose but never get logged on – so far script only identified 1 account.

    My manager know wants to see those "known" or "whitelisted" accounts displayed in the email under a heading "Known accounts" or "White listed accounts"

    But, not sure how to go about scripting that. I don't know if I can do another "ELSE" statement.
    Here is the script that's currently running:
    ######################################################################
    # Please Configure the following variables....
    $smtpServer="mail"
    $from = "Contoso IT "
    $emailaddress = "[email protected]"
    $subject = "Contoso AD users that have not logged on in 30 days"
    [string]$body = " "
    #
    #######################################################################

    #Find all AD users that have not logged on in 30 days.
    #######################################################################
    #The SEARCHBASE starts in "OU=Accounts" but excludes the following OUs:
    #Service, Shared, Inactive, Special and NDR account.
    #######################################################################

    $accounts = Search-ADAccount -AccountInactive -UsersOnly -TimeSpan 30.00:00:00 -SearchBase "OU=Accounts,DC=HQ,DC=Contoso,DC=Com" | ? {$_.DistinguishedName -notlike "*OU=Services,OU=Accounts,*" -and $_.DistinguishedName -notlike "*OU=Shared,OU=Accounts,*" -and $_.DistinguishedName -notlike "*OU=Inactive,OU=Accounts,*" -and $_.DistinguishedName -notlike "*OU=Special,OU=Accounts,*" -and $_.DistinguishedName -notlike "CN=NDR Account*"}

    If ($accounts -eq $null) {

    Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body "No users found." -priority High

    }

    Else {

    foreach ($account in $accounts) {
    $body = $body + $account.name + "`r`n"}

    Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -Body $body -priority High
    }

    In the above script, I'm excluding the "NDR account" from being reported on. How can I still report that known account in the email under a heading "Known accounts" in the body of the email?

    Thanks.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.