none
PowerShell - Results to the body of the email RRS feed

  • Question

  • # From address of email notification
    [String]$emailFromAddress = "someone@company.com"
    # To address of email notification
    [String]$emailToAddress = "someone@company.com"
    # Subject of email notification
    [String]$emailSubject = "Subject"
    # Exchange server
    [String]$emailMailserver = "someserver.company.com"
    
    
    $names = Get-ADUser -Filter 'SamAccountName -like "ahs_*"'
    
    foreach($name in $names){
    
    function Get-ADUserLastLogon([string]$userName)
    {
      $dcs = Get-ADDomainController -Filter {Name -like "*"}
      $time = 0
      foreach($dc in $dcs)
      { 
        $hostname = $dc.HostName
        $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
        if($user.LastLogon -gt $time) 
        {
          $time = $user.LastLogon
        }
      }
      $dt = [DateTime]::FromFileTime($time)
      $Body = Write-Host $username "last logged on at:" $dt | Out-String }
    
      Get-ADUserLastLogon -UserName $name
      }
    
    # Send the results in an email
    Send-MailMessage -From $emailFromAddress -To $emailToAddress -Subject $emailSubject -Body $Body -SmtpServer $emailMailserver
    I am new to PowerShell and am having an issue with a script.  Please help. I am trying to pull specific user's from AD and determine when their last logon time was and then email the output to a group.  The script will collect the information I want and it will display it on the console but it always fails to send the email as:

    Send-MailMessage : Cannot validate argument on parameter 'Body'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.


    • Moved by Bill_Stewart Monday, March 7, 2016 10:36 PM This is not "fix script I found on the Internet for me" forum
    Friday, February 5, 2016 3:30 PM

All replies

  • You cannot use Write-Host, that is for outputting to the console and nothing else. Change it to Write-Output.

    • Proposed as answer by TPC1980 Friday, February 5, 2016 5:45 PM
    Friday, February 5, 2016 3:34 PM
  • Since you are passing $Body as a variable you don't need to put the write-host or out-string. Try: 
    $Body = "$username last logged on at:$dt"}
    Friday, February 5, 2016 5:48 PM
  • So I gave up on getting it to add the results to the body so I modified it to send an attached file with the results. Thank you for your help!

    # From address of email notification
    [String]$emailFromAddress = "someone@company.com"
    # To address of email notification
    [String]$emailToAddress = "someone@company.com"
    # Subject of email notification
    [String]$emailSubject = "subject"
    # Exchange server
    [String]$emailMailserver = "server.company.com"
    # Attachment Name
    [String]$attachment = "C:\whatever.txt"
    # Body text
    [String]$Body = "The attached text file contains the report"
    
    # Identify the users you need
    $names = Get-ADUser -Filter 'SamAccountName -like "ahs_*"'
    # select a DC to use
    $dc = "servername"
    
    foreach($name in $names){
    
    function Get-ADUserLastLogon([string]$userName)
      {   
        $hostname = $dc.HostName
        $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
        if($user.LastLogon -gt $time) 
         {
          $time = $user.LastLogon
         }
       $dt = [DateTime]::FromFileTime($time)
       "$username 'last logged on at:' $dt" | Out-File -Append C:\whatever.txt 
      }
        Get-ADUserLastLogon -UserName $name
    }
    
    # Send the results in an email
    Send-MailMessage -From $emailFromAddress -To $emailToAddress -Subject $emailSubject -Attachments $attachment -Body $Body -SmtpServer $emailMailserver

    Friday, February 5, 2016 9:29 PM