User Mailbox Inventory

Welcome Forums General PowerShell Q&A User Mailbox Inventory

Viewing 9 reply threads
  • Author
    Posts
    • #215829
      Participant
      Topics: 2
      Replies: 4
      Points: 32
      Rank: Member

      Hey,

      The following code allowed me to obtain what i needed but i wondered if there was a better way to structure my query? Basically i am looking to pull out the LastLogonTime for mailbox users on Exchange as a means of identifying stale accounts, the below gives me what i need albeit i manipulate some of the rows in the spreadsheet after (to show nothing older than Feb 2020).

      Get-MailboxStatistics -Server Exchange2010 | Sort LastLogonTime -Descending | Select DisplayName,LastLogonTime | Export-CSV C:\Test.csv

    • #215841
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      You can use a Where-Object filter to limit the output of what you’re after:

      $Limit = Get-Date '1/2/2020'
      Get-MailboxStatistics -Server Exchange2010 | 
          Where-Object {(Get-Date $_.LastLogonTime) -lt $Limit} |
              Sort-Object -Property LastLogonTime -Descending | 
                  Select-Object -Property DisplayName,LastLogonTime | 
                      Export-CSV -Path C:\Test.csv -NoTypeInformation

      Depending on your locale setting is the first of February either ‘2/1/2020’ or ‘1/2/2020’. 😉

    • #215859
      Participant
      Topics: 2
      Replies: 4
      Points: 32
      Rank: Member

      Hey Olaf

      Thanks for the Feedback. The code makes sense, however it returns all results and not just those post 01/02/20 – same output as the command i used in effect. I tried with gt, le and ge but these came up blank.

      Thanks

      Tony

    • #215862
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      As Olaf stated, (my kids and us love you btw,) where-object can filter the output. I’ve captured the cutoff date in a variable after casting to datetime. I tested with both 2020-02-01 and 02-01-2020. You should be able to use the -filter parameter on Get-MailboxStatistics to optimize the speed. With the Where-Object filtering, all output is gathered and then the filter is applied. I did not figure out the filter with the date yet, if I do i’ll come post it.

      $cutoffdate = [datetime]"02-01-2020";
      Get-MailboxStatistics -Server Exchange2010 |
          where lastlogontime -gt $cutoffdate |
              select DisplayName,LastLogonTime,LastLogoffTime,ItemCount,TotalItemSize,DatabaseName |
                  Export-CSV C:\Temp\Test.csv -NoTypeInformation
      

      I’ve grabbed a few other properties that may be useful. I really hope this helps.

      • This reply was modified 1 month, 2 weeks ago by Doug Maurer.
      • This reply was modified 1 month, 2 weeks ago by Doug Maurer.
      • This reply was modified 1 month, 2 weeks ago by Doug Maurer.
      • This reply was modified 1 month, 2 weeks ago by Doug Maurer.
    • #215880
      Participant
      Topics: 2
      Replies: 4
      Points: 32
      Rank: Member

      Thanks Doug, doesnt work for me im afraid – cannot bind LastLogonTime to the $cutoffdate variable (presumably this is because the variable is DateTime and LastLogonTime is string so cant match).

      Appreciate the input from you guys here.

    • #215889
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Thanks Doug, doesnt work for me im afraid – cannot bind LastLogonTime to the $cutoffdate variable (presumably this is because the variable is DateTime and LastLogonTime is string so cant match).

      If you’re copying/pasting the code, beware that it combines the first line with the rest. I’ve added a semicolon at the end of it for others that may try to copy/paste. I also changed the path to C:\Temp\ as even with the exchange shell “ran as admin” it denied write access to C for me. Let me know if this works now. It works fine on my exchange server.

      • This reply was modified 1 month, 2 weeks ago by Doug Maurer.
    • #215919
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      cannot bind LastLogonTime to the $cutoffdate variable (presumably this is because the variable is DateTime and LastLogonTime is string so cant match).

      If that’s the case you can convert the string into a DateTime object with Get-Date. … I changed the code in my answer above … try it now. 😉

    • #216660
      Participant
      Topics: 2
      Replies: 4
      Points: 32
      Rank: Member

      Guys thanks for the feedback here. I have tried both methods and neither seem to be working right for me.

      Im due to patch this Exchange server tonight, its way out of date so potentially that is resulting in this bugging out with your commands if they are testing fine for you.

    • #216681
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      How are you running it? If you’re pasting it into powershell I’ve seen it go in out of order. If you haven’t, try opening ISE and pasting it into a blank script. Then highlight and run selection. You could also try saving it as a PS1 file and running it that way. Lastly, you can run the commands one at a time to see what isn’t working. Does get-mailboxstatistics work by itself? Maybe the command isn’t available in the powershell session you’re attempting it in?

    • #216684
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      ….. with your commands if they are testing fine for you.

      I do not have access to an Exchange environment to test at the moment. You may debug these few lines yourself.

Viewing 9 reply threads
  • You must be logged in to reply to this topic.