添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

In my script test.ps1 I try to calculate the difference in time by using the New-TimeSpan function:

$foo = "00:06:45.0"
$bar = "00:10:44.0"
$StartDate=[datetime]"01/01/2018 $foo"
$EndDate=[datetime]"01/01/2018 $bar"
Write-Output ($StartDate)
Write-Output ($EndDate)
NEW-TIMESPAN –Start $StartDate –End $EndDate

When I run the script, I get the (German) error:

New-TimeSpan : Es wurde kein Positionsparameter gefunden, der das Argument "â€Start 01/01/2018 00:06:45 â€End"
akzeptiert.
In C:\Users\me\Downloads\test.ps1:8 Zeichen:1
+ NEW-TIMESPAN –Start $StartDate –End $EndDate
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [New-TimeSpan], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewTimeSpanCommand

What makes me wonder are the strange characters in the string �!

When I enter all the commands step by step in the PowerShell directly, it works without any problems.

Do you know what could be the issue here? Thanks!

That is U+2013 EN DASH, and that's not the same thing as -, U+002D HYPHEN-MINUS. PowerShell does accept it if it can make sense of the encoding (it works from the command line) but your .ps1 is probably saved as UTF-8 without a byte order mark, causing it to be wrongly detected as Windows-1252 (or whatever your system's default encoding is). Save the file as Unicode, UTF-8 without a BOM or (arguably best) avoid Unicode dashes. – Jeroen Mostert Nov 15 '18 at 13:23 Oh for God's sake! I knew it could only be a minor mistake but I couldn't find it. I'm a copy&paste guy and if I would have typed the command myself with my keyboard I wouldn't have used the wrong dash... thank you so very much! – Erando Nov 15 '18 at 13:35 site design / logo © 2020 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2020.2.12.36053