DCDIAG E-mail Reports

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Paul Johnston Paul Johnston 1 year, 11 months ago.

  • Author
    Posts
  • #21221
    Profile photo of Paul Johnston
    Paul Johnston
    Participant

    I currently have the below code that runs DCDIAG on all of our domain controllers and e-mails me the results.

    $Sender = 'DCDiagReports@Contoso.com'
    $Recipients = 'user1@contoso.com'
    $MessageSubject = 'DCDiag Reports'
    $Relay = 'relay.contoso.local'
    $DcDiag = Get-ADDomainController -Filter * | ForEach-Object {DCDiag /s:$($_.HostName)}
    
    $email = @{
    From = $Sender
    To =$Recipients
    Subject = $MessageSubject
    SMTPServer = $Relay
    Body = ($dcdiag | Out-String)
    }
    
    send-mailmessage @email
    

    The issue I have is that the results for four domain controllers is put into one e-mail and is difficult to read. I would like to get a separate e-mail for each DC and have tried a few different things but can't seem to get it to work...

  • #21222
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    You just need to re-arrange you code to put the send-mailmessage inside the foreach loop

    $Sender = 'DCDiagReports@Contoso.com'
    $Recipients = 'user1@contoso.com'
    $MessageSubject = 'DCDiag Reports'
    $Relay = 'relay.contoso.local'
    Get-ADDomainController -Filter * |
    ForEach-Object {

    $dcdiag = DCDiag /s:$($_.HostName)

    $email = @{
    From = $Sender
    To =$Recipients
    Subject = $MessageSubject
    SMTPServer = $Relay
    Body = ($dcdiag | Out-String)
    }

    send-mailmessage @email
    }

    should work

  • #21224
    Profile photo of Paul Johnston
    Paul Johnston
    Participant

    Brilliant, exactly what I needed. Thanks Richard!

You must be logged in to reply to this topic.