none
Count active users and email report

    Question

  • Hi

    I am in need of a script that can create a count and list of users that have logged on in the last 40 days from within a specific organisational unit in active directory and have it emailed to me.

    The preferred language would be PowerShell as I have a basic understanding of this, is this possible?

    Many thanks

    Booms

    Thursday, September 19, 2013 11:58 PM

Answers

  • Last freebie (untested):

    # Create empty array for output
    $out = @()
    
    # Counter
    $count = 0
    
    # Get all AD users in the specified OU
    $users = Get-ADUser -Filter * -Properties LastLogonDate -SearchBase 'OU=Domain Users,DC=domain,DC=local'
    
    # Loop through each user
    ForEach ( $user in $users ) {
    
        # Set the cutoff amount of days here
        $dateLimit = (Get-Date).AddDays(-40)
    
        # Test if the user has logged in within the cutoff period
        If ( $user.LastLogonDate -ge $dateLimit ) {
    
            # Select username and LastLogonDate attributes (You can add here if you need more attributes)
            $props = @{
                Username=$user.SamAccountName
                LastLogonDate=$user.LastLogonDate
                }
        
            # Create custom object with the above properties
            $out += New-Object PsObject -Property $props
    
            # Increment counter
            $count++
    
        }
    
    }
    
    # This section sends the output as an email. Adjust the variables as appropriate for your environment.
    $SmtpServer = 'smtp.domain.com'
    $From = 'user@domain.com'
    $To = 'otheruser@domain.com'
    $Subject = "$count users have been active in the last 40 days"
    
    # Create the email body from the output array. You may want to add -AutoSize to the Format-Table command if you add properties.
    $Body = $out | Format-Table | Out-String
    
    # Send the email 
    Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SmtpServer


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)

    • Marked as answer by Booms Tuesday, October 22, 2013 11:36 PM
    Tuesday, October 22, 2013 10:46 PM

All replies

  • Hi Booms,

    Sure, it's definitely possible. Use Get-ADUser's -SearchBase parameter to limit Get-ADUser to just those users. Include the LastLogonDate property when using Get-ADUser too.

    Once you have your users, you can then loop through them testing for the date difference you're after (Something like if LastLogonDate -lt (Get-Date).AddDays(-40) should work). Once you've got your output from the loop, use Send-MailMessage to send it via email.

    Here's a link to the syntax for Get-ADUser and Send-MailMessage:

    http://ss64.com/ps/get-aduser.html

    http://ss64.com/ps/send-mailmessage.html

    Sound good?

    EDIT: David has provided a great example below, make sure you look that over.


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 11,925+ strong and growing)


    Friday, September 20, 2013 12:24 AM
  • Yep, that shouldn't be too hard.  Here's a way to get a basic report into a string variable, which you can then mail out with the Send-MailMessage cmdlet:

    $date = (Get-Date).AddDays(-40).ToFileTime()
    
    $params = @{
        Filter = "LastLogonDate -ge $date"
        SearchBase = 'OU=UserAccounts,DC=testdomain,DC=local'
        Properties = 'LastLogonDate'
    }
    
    $report = Get-ADUser @params |
              Format-Table SamAccountName,LastLogonDate |
              Out-String
    

    Friday, September 20, 2013 12:25 AM
  • Hi

    Thanks for the help. I unfortunately may have over estimated my ability to understanding of PowerShell. I have taken this code and put it into a .ps1 file and run from PowerShell but I get no output. I have also tried pasting it to the PowerShell window but with the same result. I did change the SearchBase to match the ou that I want to run the report on.

    Could you point me in the right direction.

    Thanks

    Wednesday, September 25, 2013 11:05 PM
  • "Out-String"? Why? What does that accomplish?

    "You put five rednecks on a mower, it's gonna be epic."
         - T.Cruz being Mr. Stupid.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, September 26, 2013 12:01 AM
    Wednesday, September 25, 2013 11:59 PM
  • The original post asked about emailing the report.  Send-MailMessage accepts a string for the message body, so my example created a string representation of the report.
    Thursday, September 26, 2013 12:02 AM
  • Okay, I had something very close to this already open in the ISE, so I adjusted it a bit. How's this?

    # Create empty array for output
    $out = @()
    
    # Get all AD users in the specified OU
    $users = Get-ADUser -Filter * -Properties LastLogonDate -SearchBase 'OU=Domain Users,DC=domain,DC=local'
    
    # Loop through each user
    ForEach ( $user in $users ) {
    
    # Set the cutoff amount of days here
    $dateLimit = (Get-Date).AddDays(-40)
    
    # Test if the user has logged in within the cutoff period
    If ( $user.LastLogonDate -ge $dateLimit ) {
    
        # Select username and LastLogonDate attributes (You can add here if you need more attributes)
        $props = @{
            Username=$user.SamAccountName
            LastLogonDate=$user.LastLogonDate
            }
        
        # Create custom object with the above properties
        $out += New-Object PsObject -Property $props
    
        }
    
    }
    
    # This section sends the output as an email. Adjust the variables as appropriate for your environment.
    $SmtpServer = 'smtp.domain.com'
    $From = 'user@domain.com'
    $To = 'otheruser@domain.com'
    $Subject = 'These users have been active in the last 40 days'
    
    # Create the email body from the output array. You may want to add -AutoSize to the Format-Table command if you add properties.
    $Body = $out | Format-Table | Out-String
    
    # Send the email 
    Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SmtpServer


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,000+ strong and growing)

    Thursday, September 26, 2013 12:04 AM
  • The original post asked about emailing the report.  Send-MailMessage accepts a string for the message body, so my example created a string representation of the report.

    Oh -  cool.  My bad!

    ¯\_(ツ)_/¯

    Thursday, September 26, 2013 12:16 AM
  • This works very well I have one more tweek that I need to achieve. I need to get a total number of users that the script finds inserted into the subject line of the email can this be achieved?
    Tuesday, October 22, 2013 10:01 PM
  • This works very well I have one more tweek that I need to achieve. I need to get a total number of users that the script finds inserted into the subject line of the email can this be achieved?

    What have you tried?

    ¯\_(ツ)_/¯

    Tuesday, October 22, 2013 10:19 PM
  • Unfortunately I have limited skills in this area and don't know where to start
    Tuesday, October 22, 2013 10:24 PM
  • Have you considered hiring a consultant or asking your network and IT people for help?

    You seem to be assuming that this is a free script writing forum.  We can help you write and fix scripts but you need to write and manage the script for yourself.

    The question you are asking is trivial enough that you should be able to look up how to do it.  It sounds like a good place for you to start learning how to write a script.

    Start here: http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx


    ¯\_(ツ)_/¯

    Tuesday, October 22, 2013 10:41 PM
  • Last freebie (untested):

    # Create empty array for output
    $out = @()
    
    # Counter
    $count = 0
    
    # Get all AD users in the specified OU
    $users = Get-ADUser -Filter * -Properties LastLogonDate -SearchBase 'OU=Domain Users,DC=domain,DC=local'
    
    # Loop through each user
    ForEach ( $user in $users ) {
    
        # Set the cutoff amount of days here
        $dateLimit = (Get-Date).AddDays(-40)
    
        # Test if the user has logged in within the cutoff period
        If ( $user.LastLogonDate -ge $dateLimit ) {
    
            # Select username and LastLogonDate attributes (You can add here if you need more attributes)
            $props = @{
                Username=$user.SamAccountName
                LastLogonDate=$user.LastLogonDate
                }
        
            # Create custom object with the above properties
            $out += New-Object PsObject -Property $props
    
            # Increment counter
            $count++
    
        }
    
    }
    
    # This section sends the output as an email. Adjust the variables as appropriate for your environment.
    $SmtpServer = 'smtp.domain.com'
    $From = 'user@domain.com'
    $To = 'otheruser@domain.com'
    $Subject = "$count users have been active in the last 40 days"
    
    # Create the email body from the output array. You may want to add -AutoSize to the Format-Table command if you add properties.
    $Body = $out | Format-Table | Out-String
    
    # Send the email 
    Send-MailMessage -From $From -To $To -Subject $Subject -Body $Body -SmtpServer $SmtpServer


    Don't retire TechNet! - (Maybe there's still a chance for hope, over 12,225+ strong and growing)

    • Marked as answer by Booms Tuesday, October 22, 2013 11:36 PM
    Tuesday, October 22, 2013 10:46 PM