1
votes

I am new to Powershell (general new to coding). What I am trying to do is: A user should be able to input a date. Powershell should echo the amount of days left till next year's December 6th.

Example: Input is 20/01/2017 and the output should be the amount of days left till 06/12/2018.

It works completely fine when I do $days = New-TimeSpan -End (Get-Date -Year $inputdate3 -Month 12 -day 6) but I want to replace this cmdlet with my string.

My code is:

$inputdate = Read-Host "Please enter the current date [DD/MM/YYYY] :"
$inputdate = [DateTime]::Parse($inputdate)
$inputdate2 = $inputdate.AddYears(1)
$inputdate3 = $inputdate2.Year

$days = New-TimeSpan -Start $inputdate -End $inputdate3 -Month 12 -Day 6 | ForEach-Object {$_.days}

echo "$days"

But I get this error:

New-TimeSpan : Parameter set cannot be resolved using the specified named parameters. At C:\Users\Asli\Desktop\O2P2_Version1_2.ps1:96 char:13 + $days = New-TimeSpan -Start $inputdate -End $inputdate3 -Month 12 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.PowerShell.Commands.NewTimeSpanCommand

2
This is a parameter set issue. If you use -Start and -End, you cannot use -Month and -Day in the same command. - AdminOfThings

2 Answers

1
votes

As AdminOfThings notes in a comment, your problem is one of using incompatible parameters:

PowerShell groups parameters that can be used together with a given command into so-called parameter sets.

Only parameters all belonging to one of the parameter sets can be used together in a given invocation of a command.

In your case, -Start and -Month, for instance, belong to different parameter sets, which prompted the error you saw.

The simplest way to inspect a command's parameter sets is to use Get-Command -Syntax (you could also just pass -? to show the syntax diagram, in the context of concise command-line help, or Get-Help, in the context of richer help; a command's online help topic shows parameter sets too):

PS> Get-Command -Syntax New-TimeSpan

New-TimeSpan [[-Start] <datetime>] [[-End] <datetime>] [<CommonParameters>]

New-TimeSpan [-Days <int>] [-Hours <int>] [-Minutes <int>] [-Seconds <int>] [<CommonParameters>]

As you can see, there are 2 parameter sets: -Start and -End form one, and -Days, -Hours, -Minutes and -Seconds the other.


Note that you don't need to use New-TimeStamp at all, because subtracting two [datetime] instances implicitly yields a [timespan] instance that represent the time span (difference) between the two dates.

Therefore, you can simplify your code as follows:

$inputDate = [datetime]::Parse((Read-Host "Please enter a start date [DD/MM/YYYY]"))

$refDate = Get-Date -Day 6 -Month 12 -Year ($inputDate.Year + 1) 

$days = ($refDate - $inputDate).Days
0
votes

Try this:

$startDate = Read-Host "Please enter the current date [DD/MM/YYYY] :"
$startDate = [DateTime]::Parse($startDate)

$endDate = Get-Date -Year ($startDate.Year + 1) -Month 12 -Day 6

$days = (New-TimeSpan –Start $startDate –End $endDate).Days
$days