none
Powershell/Exchange Server 2010 - Email Stats

    Question

  • Hello, 

    I'm trying to run this script and just get the results from members of a security group. In the Q&A section I find this (my exact issue):

    • Is there a way to get stats for some users, i.e. users that are a member of a security group only?  Example would for the sales or technical support group? 
    • Written March 10, 2011
      Sure.  The stats are for the primary email addresses, so you'd just need to script getting the primay email addresses from the members of the security group, and selecting those from the result.
    • Written March 11, 2011
      You will have to forgive me as I am not good with CODE of any type (other than login scripts).  So in looking at the script I think I would have to define the group at line that says - $exch_addrs = @{} ? I would have to some how indicate the group in that line.  Is that correct or am I totally wrong? 
      
    • Written March 11, 2011
      I wouldn't try altering the stats script if you're not familiar with powershell code.  Work on writing a separate script to get the primary smtp addresses of the group members, and then use import-csv to read the stats back in and select out the ones for that group.  Post a question on the forum if you need help.  This isn't really the place to do that.

    I wrote a separate script that grabs the group members' primary smtp to output to a csv. Now what? Import-csv email_stats... or run the email stats script against my csv with piping? This is the last piece of the puzzle and I'm looking for a hint. Thanks in advance!


    • Edited by hoogenboom Thursday, March 08, 2012 6:09 PM added link to script
    Thursday, March 08, 2012 4:41 PM

Answers

  • The stats are split out into user and domain in the csv.  Unless you've got the possibility that two different users could have the same email username but different domains, you can do this:

    $stats = import-csv mailstats.csv

    $address = 'user@domain'

    $userstats = $stats | where  {$_.user -eq ($address.split('@')[0])}

    Split off the user part of the address, and then use where-object to find the stats in the table for the row that matches that user.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Thursday, March 08, 2012 7:13 PM
    Moderator

All replies

  • The original thread was about using a pre-written script posted in teh repository that you needed to use to do this.  With out that script we hav eno idea what you are asking.

    Download the original script and start from there.

    http://gallery.technet.microsoft.com/scriptcenter/bb94b422-eb9e-4c53-a454-f7da6ddfb5d6


    ¯\_(ツ)_/¯

    Thursday, March 08, 2012 4:48 PM
  • Yep, thats the one! Great script BTW
    Thursday, March 08, 2012 6:09 PM
  • The exact answer to your question is in the first post you used to start this topic.


    ¯\_(ツ)_/¯

    Thursday, March 08, 2012 6:35 PM
  • "Work on writing a separate script to get the primary smtp addresses of the group members, and then use import-csv to read the stats back in and select out the ones for that group. Post a question on the forum if you need help"

    I wrote said script. I'm looking for help with the second part "use import-csv to read the stats back in and select out the ones for that group" Now I'm posting to the forum because I need help :)

    Thursday, March 08, 2012 6:47 PM
  • The stats are split out into user and domain in the csv.  Unless you've got the possibility that two different users could have the same email username but different domains, you can do this:

    $stats = import-csv mailstats.csv

    $address = 'user@domain'

    $userstats = $stats | where  {$_.user -eq ($address.split('@')[0])}

    Split off the user part of the address, and then use where-object to find the stats in the table for the row that matches that user.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Thursday, March 08, 2012 7:13 PM
    Moderator
  • Is this a separate script? If so, is the mailstats.csv the output csv file from the email stats script or my groupsmtp.csv file generated from below?

    Heres what I got so far:

    # Export csr group members to a csv


    get-adgroupmember csr | export-csv c:\scripts\groupusers.csv -NoTypeInformation


    # Export primary smtp address from groupusers.csv to groupsmtp.csv


    $UserData = @()
    foreach ($user in Import-Csv "c:\scripts\groupusers.csv")
    {
    $UserData += Get-Mailbox $user.name |Select-Object PrimarySmtpAddress
    }
    $UserData | Export-Csv -Path C:\scripts\groupsmtp.csv –NoTypeInformation

    Thursday, March 08, 2012 8:52 PM
  • Mailstats.csv is from the email stats script. 

    I'm assuming you'll have already run that before you get your group members smtp addresses.  Instead of exporting the email addresses to a .csv file, you'll want to take each one and use it to get the stats for the user from the imported .csv data (like the example I posted) and export those.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Thursday, March 08, 2012 9:17 PM
    Moderator
  • That works great for the one user address. But I'm trying to get the results from a csv. I've tried different things in the $address field. Arrays, import-csv. Do I have to use a foreach loop somewhere? Sorry for the beginner questions. I feel like the answer is dead simple. 

    $stats = import-csv email_stats_2012_03_06.csv
    $address = 'user@domain.com'
    $userstats = $stats | where-object  {$_.user -eq ($address.split('@')[0])}
    $userstats | Export-Csv -Path C:\scripts\userstats.csv –NoTypeInformation

    Friday, March 09, 2012 4:28 PM
  • Yes, a foreach loop is exactly what you need.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Friday, March 09, 2012 4:31 PM
    Moderator