?
Solved

PowerGui script that will display the last time a user accessed mailbox

Posted on 2010-01-11
11
Medium Priority
?
2,026 Views
Last Modified: 2012-05-08
I have a script configured that queries all my Exchange 2003 SP2 servers and display a list of mailboxes that are inactive for more than 60 days.  Whenever I execute the script the operation stops running due to the two errors listed below:
1.  Exception calling "ConvertToDateTime" with "1" argument(s): "Exception calling "ToDateTime" with "1" argument(s): "Specified argument was out of the range of valid values. Parameter name: dmtfDate"" (Select-Object)    At line: 5 char: 105

2.  Cannot convert argument "1", with value: "", for "op_Subtraction" to type "System.TimeSpan": "Cannot convert null to type "System.TimeSpan"."    At line: 5 char: 208

Here is the script:
$Servernames='server01','server02','server03','server04'
Get-WmiObject Exchange_Mailbox -Namespace root\MicrosoftExchangeV2 -ComputerName @($Servernames)| select MailboxDisplayName,@{n="LastLogonTime";e={$_.ConvertToDateTime($_.LastLogonTime)}},LastLoggedOnUserAccount,StorageGroupName,StoreName,ServerName | Where-Object { ((Get-Date)-$_.LastLogonTime).Days -gt 60}  
0
Comment
Question by:jahhan
  • 5
  • 5
11 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 26284948

Has the date conversion worked in the past? WMI has it's own that I've always found more effective for this. Alas I have no Exchange 2003 system to test this on these days so you'll have to help me out there.

Chris
0
 

Author Comment

by:jahhan
ID: 26285443
Chris-Dent,
This has never worked in the past.   I first implemented this script last week, and ever since its first execution I have received this error message.  It displays information from several mailboxes, but its never over 5 records.  I have about 100 mailboxes that should be displayed, but the script stop running after it hits the first server that generates the error.

SatyaPathak,
My environment is 2003 so I cannot use the powerpack that is listed in the URL.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 71

Expert Comment

by:Chris Dent
ID: 26285457

Fancy trying this one? I wrote it for 2003 quite a while ago:

http://www.indented.co.uk/2009/01/22/exchange-2003-get-mailboxstatistics/

If it doesn't accept your array of servers as an argument let me know and I'll modify it. Otherwise it'll just need the Where-Object part, and possibly a Select-Object if you want fewer fields.

Chris
0
 

Author Comment

by:jahhan
ID: 26285994
Chris-Dent,
I'm a newbie to powerscript.   I copied the script from the website, do I have to modify anything to have it reach out to my Exchange boxes (Example change modify the $Servername= to read $Servername=Server01)?  Do I need to install the Exchange 2007 Power Management Script?
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 26286055

No and no :)

Copy and paste the function into PowerShell as is.

Lets see if this will combine it properly with your snippet above.

Chris
$Servernames= 'server01', 'server02', 'server03', 'server04'

$Servernames | %{ Get-2003MailboxStatistics -Server $_ } | `
  Select-Object MailboxDisplayName, LastLogonTime, LastLoggedOnUserAccount, `
    StorageGroupName, StoreName, ServerName | `
  Where-Object { $_.LastLogonTime -gt (Get-Date).AddDays(-60) }

Open in new window

0
 

Author Comment

by:jahhan
ID: 26286155
Below is the error message I receive:
The term 'Get-2003MailboxStatistics' is not recognized as a cmdlet, function, operable program, or script file. Verify the term and try again.      At line: 6 char: 44            
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 26287040

You pasted the function from my blog in prior to that? It needs to be loaded into the shell before it can be used.

It makes the full code into this.

Chris
Function Get-2003MailboxStatistics {
  Param(
     [String]$Identity = "",
     [String]$Server = $Env:ComputerName,
     [String]$Database = ""
  )

  $Filter = "ServerName='$Server'"
  if ($Database -ne "" ) {
    $Filter = "$Filter AND StoreName='$Database'"
  }
  if ($Identity -ne "") {
    $Filter = "$Filter AND (MailboxGuid='{$Identity}' OR LegacyDN='$Identity'"
    $Filter = "$Filter OR MailboxDisplayName='$Identity')"
  }

  Get-WMIObject -ComputerName $Server `
    -Namespace "root/MicrosoftExchangeV2" -Class "Exchange_Mailbox" `
    -Filter $Filter | `
  Select-Object `
    AssocContentCount, `
    @{n='DateDiscoveredAbsentInDs';e={
      If ($_.DateDiscoveredAbsentInDs -ne $Null) {
        [Management.ManagementDateTimeConverter]::ToDateTime(`
          $_.DateDiscoveredAbsentInDs)
      }} }, `
    MailboxDisplayName, TotalItems, LastLoggedOnUserAccount, `
    @{n='LastLogonTime';e={ if ($_.LastLogonTime -ne $Null) { `
      [Management.ManagementDateTimeConverter]::ToDateTime($_.LastLogonTime)}}
    }, `
    @{n='LastLogoffTime';e={ if ($_.LastLogoffTime -ne $Null) { `
      [Management.ManagementDateTimeConverter]::ToDateTime($_.LastLogoffTime)}}
    }, `
    LegacyDN, `
    @{n='MailboxGuid';e={
      ([String]$_.MailboxGuid).ToLower() -Replace "\{|\}" }}, `
    @{n='ObjectClass';e={ "Mailbox" }}, `
    @{n='StorageLimitStatus';e={ `
      Switch ($_.StorageLimitInfo) {
        1 { "BelowLimit" }
        2 { "IssueWarning" }
        4 { "ProhibitSend" }
        8 { "NoChecking" }
        16 { "MailboxDisabled" } }} }, `
    DeletedMessageSize, Size, `
    @{n='Database';e={
      "$($_.ServerName)\$($_.StorageGroupName)\$($_.StoreName)" }}, `
    ServerName, StorageGroupName, StoreName, `
    @{n='Identity';e={ ([String]$_.MailboxGuid).ToLower() -Replace "\{|\}" }}
}

$Servernames= 'server01', 'server02', 'server03', 'server04'

$Servernames | %{ Get-2003MailboxStatistics -Server $_ } | `
  Select-Object MailboxDisplayName, LastLogonTime, LastLoggedOnUserAccount, `
    StorageGroupName, StoreName, ServerName | `
  Where-Object { $_.LastLogonTime -gt (Get-Date).AddDays(-60) }

Open in new window

0
 

Author Comment

by:jahhan
ID: 26288309
So I plugged in this script and it is now asking me for my identity, server, and database.  Is this suppose to happen?  If so, I was looking for a script that retrieves all my mailboxes without typing in anything.
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 26291145

You mean it's prompting you for those? Shouldn't be, it has default values for each and they're all optional not mandatory.

Well never mind, it's only quicker if it works for you, saved me having to write the same code again. Here's a new version built only to do this task.

Chris
$Servernames= 'server01', 'server02', 'server03', 'server04'

Get-WMIObject -ComputerName $Servernames `
    -Namespace "root/MicrosoftExchangeV2" -Class "Exchange_Mailbox" | `
  Select-Object MailboxDisplayName, `
    @{n='LastLogonTime';e={ if ($_.LastLogonTime -ne $Null) {
      [Management.ManagementDateTimeConverter]::ToDateTime($_.LastLogonTime)}} }, `
    LastLoggedOnUserAccount, StorageGroupName, StoreName, ServerName | `
  Where-Object { $_.LastLogonTime -gt (Get-Date).AddDays(-60) }

Open in new window

0
 

Author Closing Comment

by:jahhan
ID: 31675536
Chris-Dent,
Thank you so much for your help.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post, we will learn to set up the Group Naming policy and will see how it is going to impact the Display Name and the Email addresses of the Group.
Welcome to 2018! Exciting things lie ahead in the world of tech. To start things off, we compiled great member articles on how to stay safe, ways to learn, and much more! Read on to start your new year right.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

569 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question