rubrikinc / rubrik-sdk-for-powershell

Rubrik Module for PowerShell
https://build.rubrik.com/sdks/powershell/
MIT License
102 stars 87 forks source link

Convert-APIDateTime requires better error handling #836

Open tonypags opened 1 year ago

tonypags commented 1 year ago

Is your feature request related to a problem? Please describe.

String date conversion fails a lot, and there are no error messages, just null outputs.

Describe the solution you'd like

I forked the code and found the issue in Convert-APIDateTime. I added handling for a 2nd string format in the function, as well as an else condition for unsupported strings (via RegEx matching).

Also, I added a method to the date object to ensure it retains its universal "Kind" time property. Otherwise it was being left ambiguous.

Also added tests for these changes, to the existing test file.

Describe alternatives you've considered

I can PR this branch I made. here

Additional context

I was getting a massive amount of errors like this in my logs:

Exception calling "ParseExact" with "3" argument(s): "String was not recognized as a valid DateTime."
At C:\Program Files\WindowsPowerShell\Modules\Rubrik\6.0.1\Private\Convert-APIDateTime.ps1:45 char:13
+             [DateTime]::ParseExact($NewDateTimeString,'MM dd HH:mm:ss ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : FormatException

I added better error handling so I could get a sample of the invalid string in the message.

Convert-APIDateTime : -07-16T10:02:33.896Z: Exception calling "ParseExact" with "3" argument(s): 
"String was not recognized as a valid DateTime."
At C:\Program Files\WindowsPowerShell\Modules\Rubrik\6.0.1\Public\Get-RubrikEvent.ps1:231 char:25
+           expression = {Convert-APIDateTime -DateTimeString $_.time}
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Convert-APIDateTime

This date string is not in the format of the one the function was expecting. This is a standard datetime format that Get-Date will recognize as UTC, but the first 4 chars were truncated by the function.

I added a conditional to support it, and errors for any future unsupported cases.

        if ($DateTimeString -match '^\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2} UTC \d{4}$') {

            $NewDateTimeString = $DateTimeString.Substring(4) -replace 'UTC '
            $MonthHash.GetEnumerator() | ForEach-Object {
                $NewDateTimeString = $NewDateTimeString -replace $_.Key,$_.Value
            }
            try {
                [DateTime]::ParseExact($NewDateTimeString,'MM dd HH:mm:ss yyyy',$null).ToLocalTime().ToUniversalTime()
            } catch {
                Write-Error "$($NewDateTimeString): $($_.Exception.Message)"
            }

        } elseif ($DateTimeString -match '^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$') {

            try {
                (Get-Date $DateTimeString -ea Stop).ToUniversalTime()
            } catch {
                Write-Error "$($DateTimeString): $($_.Exception.Message)"
            }

        } else {
            Write-Error "Unhandled date string format: $($DateTimeString)"
        }