Problem with get-date

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 10 months ago.

  • Author
    Posts
  • #5520

    by gbritton at 2013-02-06 09:37:14

    Hi — I'm having trouble grasping how to use get-date in a function. e.g.

    function d($f, $t) {
    $f;$t
    get-date $f
    get-date $t
    }

    But when I call the function like this:

    $a=get-date '1 jan 2013'
    $b=get-date '31 jan 2013'

    d($a,$b)

    I get these messages:

    [list]Tuesday, January 01, 2013 12:00:00 AM
    Thursday, January 31, 2013 12:00:00 AM
    Get-Date : Cannot convert 'System.Object[]' to the type 'System.DateTime' requi
    red by parameter 'Date'. Specified method is not supported.
    At line:4 char:10
    + get-date < <<< $f
    + CategoryInfo : InvalidArgument: (:) [Get-Date], ParameterBindin
    gException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Comma
    nds.GetDateCommand

    Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date"
    : "Object reference not set to an instance of an object."
    At line:5 char:10
    + get-date < <<< $t
    + CategoryInfo : WriteError: (:) [Get-Date], ParameterBindingExce
    ption
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Comm
    ands.GetDateCommand[/list]

    How can I use get-date in my toy function without generating errors?

    Note that, if I just issue get-date interactively, it works just fine:

    [list]
    PS Microsoft.PowerShell.Core\FileSystem::\\mcmsd-icms01\BillOps Data\Billing Operations\Tollse> $a=get-date '1 jan 2013'
    $b=get-date '31 jan 2013'

    ____________________________________________________________________________
    PS Microsoft.PowerShell.Core\FileSystem::\\mcmsd-icms01\BillOps Data\Billing Operations\Tollse> get-date $a

    Tuesday, January 01, 2013 12:00:00 AM

    ____________________________________________________________________________
    PS Microsoft.PowerShell.Core\FileSystem::\\mcmsd-icms01\BillOps Data\Billing Operations\Tollse> get-date $b

    Thursday, January 31, 2013 12:00:00 AM
    [/list]

    by ArtB0514 at 2013-02-06 10:17:21

    The problem is in how you are calling the function d. It is expecting 2 parameters, but by combining the input objects in parens, you are feeding it only one. And that one (going to the $f variable) is not in any format that Get-Date knows how to process, hence the first error message. The second error message comes from the $t variable being empty.

    Try it this way:

    function d($f, $t) {
    $f;$t
    get-date $f
    get-date $t
    }
    $a=get-date '1 jan 2013'
    $b=get-date '31 jan 2013'

    d $a $b

    by ArtB0514 at 2013-02-06 10:30:11

    You can get lots of information from Get-Help About_Functions (and the SEE ALSO topics at the bottom). Actually, use the -Online switch so you can get it all in a web browser and more easily navigate through it all.
    Get-Help About_Functions -Online

    by gbritton at 2013-02-06 10:43:03

    Aargh! You're right! I'm still getting used to PowerShell's unique syntax for function calling. I guess it's too late to change that? FWIW many (most?) languages have some symmetry between a function's signature and calling syntax. No wonder I'm confused...

    by ArtB0514 at 2013-02-06 11:38:04

    The form you used was from PowerShell 1 and is no longer considered good practice. Look up help for About_AdvancedFunctions. Here's a simplified way to define a function using the more modern syntax that should help:

    function d {
    param(
    [datetime]$f,
    [datetime]$t
    )
    $f
    $t
    get-date $f
    get-date $t
    }

    This way you call the function using either the BOA-Constructor method (d $a $b), or the parameter specific method (d -f $a -t $b)

    by gbritton at 2013-02-06 12:15:54

    Thanks!

You must be logged in to reply to this topic.