Problem with get-date

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) {
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’


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: (:slight_smile: [Get-Date], ParameterBindin
+ FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Comma

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: (:slight_smile: [Get-Date], ParameterBindingExce
+ FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Comm

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:

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
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) {
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 {
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