12Knocksinna / Office365itpros

Office 365 for IT Pros PowerShell examples
MIT License
1.24k stars 559 forks source link

ReportUnusedExoMailboxes.PS1 "System.DateTime". Error: "String was not recognized as a valid DateTime." #101

Closed albert-widjaja closed 5 months ago

albert-widjaja commented 5 months ago

Greetings @12Knocksinna ,

When I execute the script ReportUnusedExoMailboxes.PS1 I got this error like the below:

New-TimeSpan : Cannot bind parameter 'Start'. Cannot convert value "1/16/2024 1:19:14 PM" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

Despite I have modified this section: $Now = Get-Date -Format 'MM/dd/yyyy hh:mm:ss tt'

Thank you.

12Knocksinna commented 5 months ago

Just make sure that the data being compared is a date. I've updated the script to do this.

albert-widjaja commented 5 months ago

Thank you for the quick update @12Knocksinna , however it is still doesn't work for the Non US time zone?


Get-Date : Cannot bind parameter 'Date'. Cannot convert value "7/18/2023 5:01:47 AM" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."
At C:\ReportUnusedExOMailboxes.PS1:34 char:50
+         [datetime]$LastActiveDateTime = Get-Date $LastActive
+                                                  ~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetDateCommand

New-TimeSpan : Cannot bind parameter 'Start' to the target. Exception setting "Start": "Cannot convert null to type "System.DateTime"."
At C:\ReportUnusedExOMailboxes.PS1:35 char:49
+ ...   $DaysSinceActive = (New-TimeSpan -Start $LastActiveDateTime -End $N ...
+                                               ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand

PS C:\Windows\system32\WindowsPowerShell\v1.0>  $LastActive
7/18/2023 5:01:47 AM

PS C:\Windows\system32\WindowsPowerShell\v1.0> $LastActiveDateTime

PS C:\Windows\system32\WindowsPowerShell\v1.0> (New-TimeSpan -Start $LastActiveDateTime -End $Now).Days
New-TimeSpan : Cannot bind parameter 'Start' to the target. Exception setting "Start": "Cannot convert null to type "System.DateTime"."
At line:1 char:22
+ (New-TimeSpan -Start $LastActiveDateTime -End $Now).Days
+                      ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.NewTimeSpanCommand
12Knocksinna commented 5 months ago

Try the script now

albert-widjaja commented 5 months ago

Thank you for the quick response @12Knocksinna ,

even after I've updated the -0 into 0, there is still an error thrown like the below at line 36:

image

12Knocksinna commented 5 months ago

This is difficult because I can't see the data you're dealing with. I've updated the script again. If you find another issue, please tell me the value of the date retrieved from Export-MailboxDiagnosticLogs.

BTW, because you have all the PowerShell code, you can debug and fix the problem yourself...