How do I get the $RESULT from this function

Welcome Forums General PowerShell Q&A How do I get the $RESULT from this function

This topic contains 4 replies, has 3 voices, and was last updated by

6 months, 3 weeks ago.

  • Author
  • #105725

    Points: 322
    Rank: Contributor

    After hours of trying out various possibilities, I am at a dead end on a very simple DATE validation Function.
    The Function was copied from:

    #PSTip How to check if a DateTime string is in a specific pattern

    Here is the full text

    Function Test-DateTimePattern
            [System.Globalization.CultureInfo]$Culture = (Get-Culture),
        $result = try{ [DateTime]::ParseExact($String,$Pattern,$Culture) } catch{}
        if ($PassThru -AND $result) { $result } else { [bool]$result }

    I have been using it for several weeks now and find it very useful.
    Now I wish to use the 4th parameter $Passthru, in order to get the [DateTime] object back — note that the input is of [string] type. So I invoked the Function with the 4th parameter (switch) using $True as the input.
    My question is: WHERE is the valid [DateTime] object that is presumably returned if it parsed correctly?
    The Function returns a [boolean] object type, so I cannot figure out where/how the [DateTime] object is.

    Would be grateful for any hints, advice or tips.
    Thanking you in advance.

  • #105730

    Points: 176
    Helping Hand
    Rank: Participant

    When the PassThru switch is set to TRUE, the output type in not boolean..

    PS D:\Sandbox> Test-DateTimePattern -String '12:15 PM' -Pattern t
    PS D:\Sandbox> Test-DateTimePattern -String '12:15 PM' -Pattern t -PassThru:$true
    Wednesday, August 1, 2018 12:15:00 PM

    the valid [DateTime] object is returned on the TRUE branch of the if statement on line 10 – note that $result in this branch is not type-cast as [bool] retaining its PowerShell assigned [DateTime] type from the successful TRY statement on line 9

    • #105779

      Points: 322
      Rank: Contributor

      Thank you Sam, but when I tried to "get" the [DateTime] as follows:

      $V = Test-DateTimePattern .... (parms omitted)

      I got an error message that says I cannot assign $True to $V. It seems to me there is a chicken-and-egg situation here:

      1 – to see first if my inputs were valid, I must have a IF statement on $V after the method call.
      2 – If valid, then I access $V as the "returned valid parse", i.e., the desired [dateTime] object ... but in this case I get an error because $V according to PS is boolean.

      My question again is, How can I "get to" the RETURNED [DateTime] object? Where is it sitting? Is it via assignment to a variable? I know I get the correct [DateTime] returned when it prints on the screen but that's when I run it directly. What I want is to get hold of that [DateTime] object so I can perform more validity checks such as how old, etc.
      Hope this clarifies my dilemma/question/lack of understanding.
      Many thanks.

  • #105740

    Points: 403
    Helping Hand
    Rank: Contributor

    It's a switch; you just do -PassThru. the :$true is very rarely necessary.

    • #105782

      Points: 322
      Rank: Contributor

      Thanks Joel, but my dilemma is to get access to the RETURNED, and VALIDATED, [DateTime] object.
      So when I assign the method call to a variable, e.g.,
      $V = Test-DateTimePattern ....
      and then try to use $V as [DateTime] (e.g., .AddDays) I get an error about using a [boolean].
      Hope this clarifies. See my reply to Sam Boutros on this same thread.
      Much thanks.

The topic ‘How do I get the $RESULT from this function’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort