PowerShell - 一个与 convertfrom-string 相关的奇怪问题

Posted

技术标签:

【中文标题】PowerShell - 一个与 convertfrom-string 相关的奇怪问题【英文标题】:PowerShell - An weird issue related to convertfrom-string 【发布时间】:2021-03-17 22:21:43 【问题描述】:

因此,我尝试使用 PowerShell 作为自我强加的编程挑战来获得 21 世纪四个季节的平均长度。

我的想法是从文本文件中获取值,创建一个 [PSCustomObject] 并为其每行的 noteproperties 分配值,将年份添加到日期并将日期转换为 [datetime] 并添加到另一个数组,然后循环第二个数组使用 index 并使用 new-timespan 获取季节长度并添加到第三个数组和 measure-object 第三个数组,如果这在英语中可能听起来令人困惑,请原谅我,但它在代码中确实非常简单。

现在我从这里得到春分和至日的日期:Solstices and Equinoxes: 2001 to 2100

使用 Notepad++ 格式化我得到的日期:

2001   Mar 20  13:31   Jun 21  07:38   Sep 22  23:05   Dec 21  19:22
2002   Mar 20  19:16   Jun 21  13:25   Sep 23  04:56   Dec 22  01:15
2003   Mar 21  01:00   Jun 21  19:11   Sep 23  10:47   Dec 22  07:04
2004   Mar 20  06:49   Jun 21  00:57   Sep 22  16:30   Dec 21  12:42
2005   Mar 20  12:34   Jun 21  06:46   Sep 22  22:23   Dec 21  18:35
2006   Mar 20  18:25   Jun 21  12:26   Sep 23  04:04   Dec 22  00:22
2007   Mar 21  00:07   Jun 21  18:06   Sep 23  09:51   Dec 22  06:08
2008   Mar 20  05:49   Jun 21  00:00   Sep 22  15:45   Dec 21  12:04
2009   Mar 20  11:44   Jun 21  05:45   Sep 22  21:18   Dec 21  17:47
2010   Mar 20  17:32   Jun 21  11:28   Sep 23  03:09   Dec 21  23:38
2011   Mar 20  23:21   Jun 21  17:16   Sep 23  09:05   Dec 22  05:30
2012   Mar 20  05:15   Jun 20  23:08   Sep 22  14:49   Dec 21  11:12
2013   Mar 20  11:02   Jun 21  05:04   Sep 22  20:44   Dec 21  17:11
2014   Mar 20  16:57   Jun 21  10:52   Sep 23  02:30   Dec 21  23:03
2015   Mar 20  22:45   Jun 21  16:38   Sep 23  08:20   Dec 22  04:48
2016   Mar 20  04:31   Jun 20  22:35   Sep 22  14:21   Dec 21  10:45
2017   Mar 20  10:29   Jun 21  04:25   Sep 22  20:02   Dec 21  16:29
2018   Mar 20  16:15   Jun 21  10:07   Sep 23  01:54   Dec 21  22:22
2019   Mar 20  21:58   Jun 21  15:54   Sep 23  07:50   Dec 22  04:19
2020   Mar 20  03:50   Jun 20  21:43   Sep 22  13:31   Dec 21  10:03
2021   Mar 20  09:37   Jun 21  03:32   Sep 22  19:21   Dec 21  15:59
2022   Mar 20  15:33   Jun 21  09:14   Sep 23  01:04   Dec 21  21:48
2023   Mar 20  21:25   Jun 21  14:58   Sep 23  06:50   Dec 22  03:28
2024   Mar 20  03:07   Jun 20  20:51   Sep 22  12:44   Dec 21  09:20
2025   Mar 20  09:02   Jun 21  02:42   Sep 22  18:20   Dec 21  15:03
2026   Mar 20  14:46   Jun 21  08:25   Sep 23  00:06   Dec 21  20:50
2027   Mar 20  20:25   Jun 21  14:11   Sep 23  06:02   Dec 22  02:43
2028   Mar 20  02:17   Jun 20  20:02   Sep 22  11:45   Dec 21  08:20
2029   Mar 20  08:01   Jun 21  01:48   Sep 22  17:37   Dec 21  14:14
2030   Mar 20  13:51   Jun 21  07:31   Sep 22  23:27   Dec 21  20:09
2031   Mar 20  19:41   Jun 21  13:17   Sep 23  05:15   Dec 22  01:56
2032   Mar 20  01:23   Jun 20  19:09   Sep 22  11:11   Dec 21  07:57
2033   Mar 20  07:23   Jun 21  01:01   Sep 22  16:52   Dec 21  13:45
2034   Mar 20  13:18   Jun 21  06:45   Sep 22  22:41   Dec 21  19:35
2035   Mar 20  19:03   Jun 21  12:33   Sep 23  04:39   Dec 22  01:31
2036   Mar 20  01:02   Jun 20  18:31   Sep 22  10:23   Dec 21  07:12
2037   Mar 20  06:50   Jun 21  00:22   Sep 22  16:13   Dec 21  13:08
2038   Mar 20  12:40   Jun 21  06:09   Sep 22  22:02   Dec 21  19:01
2039   Mar 20  18:32   Jun 21  11:58   Sep 23  03:50   Dec 22  00:41
2040   Mar 20  00:11   Jun 20  17:46   Sep 22  09:44   Dec 21  06:33
2041   Mar 20  06:07   Jun 20  23:37   Sep 22  15:27   Dec 21  12:19
2042   Mar 20  11:53   Jun 21  05:16   Sep 22  21:11   Dec 21  18:04
2043   Mar 20  17:29   Jun 21  10:59   Sep 23  03:07   Dec 22  00:02
2044   Mar 19  23:20   Jun 20  16:50   Sep 22  08:47   Dec 21  05:43
2045   Mar 20  05:08   Jun 20  22:34   Sep 22  14:33   Dec 21  11:36
2046   Mar 20  10:58   Jun 21  04:15   Sep 22  20:22   Dec 21  17:28
2047   Mar 20  16:52   Jun 21  10:02   Sep 23  02:07   Dec 21  23:07
2048   Mar 19  22:34   Jun 20  15:54   Sep 22  08:01   Dec 21  05:02
2049   Mar 20  04:28   Jun 20  21:47   Sep 22  13:42   Dec 21  10:51
2050   Mar 20  10:20   Jun 21  03:33   Sep 22  19:29   Dec 21  16:39
2051   Mar 20  15:58   Jun 21  09:17   Sep 23  01:26   Dec 21  22:33
2052   Mar 19  21:56   Jun 20  15:16   Sep 22  07:16   Dec 21  04:18
2053   Mar 20  03:46   Jun 20  21:03   Sep 22  13:05   Dec 21  10:09
2054   Mar 20  09:35   Jun 21  02:47   Sep 22  19:00   Dec 21  16:10
2055   Mar 20  15:28   Jun 21  08:39   Sep 23  00:48   Dec 21  21:56
2056   Mar 19  21:11   Jun 20  14:29   Sep 22  06:40   Dec 21  03:52
2057   Mar 20  03:08   Jun 20  20:19   Sep 22  12:23   Dec 21  09:42
2058   Mar 20  09:04   Jun 21  02:03   Sep 22  18:07   Dec 21  15:24
2059   Mar 20  14:44   Jun 21  07:47   Sep 23  00:03   Dec 21  21:18
2060   Mar 19  20:37   Jun 20  13:44   Sep 22  05:47   Dec 21  03:00
2061   Mar 20  02:26   Jun 20  19:33   Sep 22  11:31   Dec 21  08:49
2062   Mar 20  08:07   Jun 21  01:10   Sep 22  17:19   Dec 21  14:42
2063   Mar 20  13:59   Jun 21  07:02   Sep 22  23:08   Dec 21  20:22
2064   Mar 19  19:40   Jun 20  12:47   Sep 22  04:58   Dec 21  02:10
2065   Mar 20  01:27   Jun 20  18:31   Sep 22  10:41   Dec 21  07:59
2066   Mar 20  07:19   Jun 21  00:16   Sep 22  16:27   Dec 21  13:45
2067   Mar 20  12:55   Jun 21  05:56   Sep 22  22:20   Dec 21  19:44
2068   Mar 19  18:51   Jun 20  11:55   Sep 22  04:09   Dec 21  01:34
2069   Mar 20  00:44   Jun 20  17:40   Sep 22  09:51   Dec 21  07:21
2070   Mar 20  06:35   Jun 20  23:22   Sep 22  15:45   Dec 21  13:19
2071   Mar 20  12:36   Jun 21  05:21   Sep 22  21:39   Dec 21  19:05
2072   Mar 19  18:19   Jun 20  11:12   Sep 22  03:26   Dec 21  00:54
2073   Mar 20  00:12   Jun 20  17:06   Sep 22  09:14   Dec 21  06:50
2074   Mar 20  06:09   Jun 20  22:59   Sep 22  15:04   Dec 21  12:36
2075   Mar 20  11:48   Jun 21  04:41   Sep 22  21:00   Dec 21  18:28
2076   Mar 19  17:37   Jun 20  10:35   Sep 22  02:48   Dec 21  00:12
2077   Mar 19  23:30   Jun 20  16:23   Sep 22  08:35   Dec 21  06:00
2078   Mar 20  05:11   Jun 20  21:58   Sep 22  14:25   Dec 21  11:59
2079   Mar 20  11:03   Jun 21  03:51   Sep 22  20:15   Dec 21  17:46
2080   Mar 19  16:43   Jun 20  09:33   Sep 22  01:55   Dec 20  23:31
2081   Mar 19  22:34   Jun 20  15:16   Sep 22  07:38   Dec 21  05:22
2082   Mar 20  04:32   Jun 20  21:04   Sep 22  13:24   Dec 21  11:06
2083   Mar 20  10:08   Jun 21  02:41   Sep 22  19:10   Dec 21  16:51
2084   Mar 19  15:58   Jun 20  08:39   Sep 22  00:58   Dec 20  22:40
2085   Mar 19  21:53   Jun 20  14:33   Sep 22  06:43   Dec 21  04:29
2086   Mar 20  03:36   Jun 20  20:11   Sep 22  12:33   Dec 21  10:24
2087   Mar 20  09:27   Jun 21  02:05   Sep 22  18:27   Dec 21  16:07
2088   Mar 19  15:16   Jun 20  07:57   Sep 22  00:18   Dec 20  21:56
2089   Mar 19  21:07   Jun 20  13:43   Sep 22  06:07   Dec 21  03:53
2090   Mar 20  03:03   Jun 20  19:37   Sep 22  12:01   Dec 21  09:45
2091   Mar 20  08:40   Jun 21  01:17   Sep 22  17:49   Dec 21  15:37
2092   Mar 19  14:33   Jun 20  07:14   Sep 21  23:41   Dec 20  21:31
2093   Mar 19  20:35   Jun 20  13:08   Sep 22  05:30   Dec 21  03:21
2094   Mar 20  02:20   Jun 20  18:40   Sep 22  11:15   Dec 21  09:11
2095   Mar 20  08:14   Jun 21  00:38   Sep 22  17:10   Dec 21  15:00
2096   Mar 19  14:03   Jun 20  06:31   Sep 21  22:55   Dec 20  20:46
2097   Mar 19  19:49   Jun 20  12:14   Sep 22  04:37   Dec 21  02:38
2098   Mar 20  01:38   Jun 20  18:01   Sep 22  10:22   Dec 21  08:19
2099   Mar 20  07:17   Jun 20  23:41   Sep 22  16:10   Dec 21  14:04
2100   Mar 20  13:04   Jun 21  05:32   Sep 22  22:00   Dec 21  19:51

然后我在 Windows 10 上的 PowerShell 7.1 x64 上使用了这些代码:

$timetable = Get-Content .\Desktop\Equinox-Solstice.txt | Convertfrom-String -Templatefile .\Desktop\template.txt
$count=$timetable.count
$timetable1=@()
for ($i=0;$i -lt $count;$i++) 
$year=[string]$timetable[$i].year
$mequi=[datetime]($year+" "+$timetable[$i].marequi)
$jsols=[datetime]($year+" "+$timetable[$i].junsols)
$sequi=[datetime]($year+" "+$timetable[$i].sepequi)
$dsols=[datetime]($year+" "+$timetable[$i].decsols)
$timetable1+=[pscustomobject]@year=$year;mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols

以此为模板:

[int]year*:2001   [string]marequi:Mar 20  13:31   [string]junsols:Jun 21  07:38   [string]sepequi:Sep 22  23:05   [string]decsols:Dec 21  19:22
[int]year*:2002   [string]marequi:Mar 20  19:16   [string]junsols:Jun 21  13:25   [string]sepequi:Sep 23  04:56   [string]decsols:Dec 22  01:15
[int]year*:2003   [string]marequi:Mar 21  01:00   [string]junsols:Jun 21  19:11   [string]sepequi:Sep 23  10:47   [string]decsols:Dec 22  07:04
[int]year*:2004   [string]marequi:Mar 20  06:49   [string]junsols:Jun 21  00:57   [string]sepequi:Sep 22  16:30   [string]decsols:Dec 21  12:42
[int]year*:2005   [string]marequi:Mar 20  12:34   [string]junsols:Jun 21  06:46   [string]sepequi:Sep 22  22:23   [string]decsols:Dec 21  18:35
[int]year*:2006   [string]marequi:Mar 20  18:25   [string]junsols:Jun 21  12:26   [string]sepequi:Sep 23  04:04   [string]decsols:Dec 22  00:22
[int]year*:2007   [string]marequi:Mar 21  00:07   [string]junsols:Jun 21  18:06   [string]sepequi:Sep 23  09:51   [string]decsols:Dec 22  06:08
[int]year*:2008   [string]marequi:Mar 20  05:49   [string]junsols:Jun 21  00:00   [string]sepequi:Sep 22  15:45   [string]decsols:Dec 21  12:04
[int]year*:2009   [string]marequi:Mar 20  11:44   [string]junsols:Jun 21  05:45   [string]sepequi:Sep 22  21:18   [string]decsols:Dec 21  17:47
[int]year*:2010   [string]marequi:Mar 20  17:32   [string]junsols:Jun 21  11:28   [string]sepequi:Sep 23  03:09   [string]decsols:Dec 21  23:38

当我运行命令时,我收到了很多错误消息,它们都是这样的:

InvalidArgument:
Line |
   3 |  $mequi=[datetime]($year+" "+$timetable[$i].marequi)
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | Cannot convert value "2093 " to type "System.DateTime". Error: "String '2093 ' was not recognized as a valid DateTime."

我仔细检查了,发现所有这些都来自 $mequi,它代表 March Equinox,我意识到日期肯定丢失了,然后我输入了 $timetable 并确认很多 March Equinox 日期都丢失了,只有 March Equinox缺少日期,然后我使用了这个命令:

for($i=0;$i -lt 99;$i++)if ($timetable[$i].marequi -eq $null)$i

发现其中46个日期缺失,但其他3个类别的日期并没有缺失,只是三月分,缺失日期的索引如下: 11, 15, 19, 23, 27, 31, 35, 39, 40, 43, 44, 47, 48, 51, 52, 55, 56, 59, 60, 63, 64, 67, 68, 69, 71, 72、73、75、76、77、79、80、81、83、84、85、87、88、89、91、92、93、95、96、97、98

有什么想法吗?有人可以帮帮我吗?

Update3:所以我使用了完整的代码:

$timetable = Get-Content .\Desktop\Equinox-Solstice.txt | Convertfrom-String -Templatefile .\Desktop\template.txt
$count=$timetable.count
[array]$timetable1 = 0..99 | foreach-object 
$year=[string]$timetable[$_].year
$mequi=[datetime]($year+" "+$timetable[$_].marequi)
$jsols=[datetime]($year+" "+$timetable[$_].junsols)
$sequi=[datetime]($year+" "+$timetable[$_].sepequi)
$dsols=[datetime]($year+" "+$timetable[$_].decsols)
[pscustomobject]@year=$year;mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols

[array]$seasons=0..98 | foreach-object 
$year=$timetable1[$_].year
$spring=[double](New-Timespan -Start $timetable1[$_].mequi -End $timetable1[$_].jsols).totaldays
$summer=[double](New-Timespan -Start $timetable1[$_].jsols -End $timetable1[$_].sequi).totaldays
$autumn=[double](New-Timespan -Start $timetable1[$_].sequi -End $timetable1[$_].dsols).totaldays
$winter=[double](New-Timespan -Start $timetable1[$_].dsols -End $timetable1[$_+1].mequi).totaldays
[pscustomobject]@year=$year;spring=$spring;summer=$summer;autumn=$autumn;winter=$winter

$meanspring=($seasons.spring | Measure-Object -Average).average
$meansummer=($seasons.summer | Measure-Object -Average).average
$meanautumn=($seasons.autumn | Measure-Object -Average).average
$meanwinter=($seasons.winter | Measure-Object -Average).average
$meanyear=$meanspring+$meansummer+$meanautumn+$meanwinter
Write-Host "Mean Spring Length: $meanspring days, Mean Summer Length: $meansummer days, Mean Autumn Length: $meanautumn days, Mean Winter Length: $meanwinter days, Mean Solar Year Length: $meanyear days"

这就是我得到的:

Mean Spring Length: 92.7203563411897 days, Mean Summer Length: 93.6684764309764 days, Mean Autumn Length: 89.8785914702581 days, Mean Winter Length: 88.9748106060606 days, Mean Solar Year Length: 365.242234848485 days.

更新:我现在正在编辑问题,因为我想分享一个更好的方法。

通过在 Notepad++ 中查找和替换的更多努力,我得到了这个:

"year","marequi","junsols","sepequi","decsols"
"2001","Mar 20  13:31","Jun 21  07:38","Sep 22  23:05","Dec 21  19:22"
"2002","Mar 20  19:16","Jun 21  13:25","Sep 23  04:56","Dec 22  01:15"
"2003","Mar 21  01:00","Jun 21  19:11","Sep 23  10:47","Dec 22  07:04"
"2004","Mar 20  06:49","Jun 21  00:57","Sep 22  16:30","Dec 21  12:42"
"2005","Mar 20  12:34","Jun 21  06:46","Sep 22  22:23","Dec 21  18:35"
"2006","Mar 20  18:25","Jun 21  12:26","Sep 23  04:04","Dec 22  00:22"
"2007","Mar 21  00:07","Jun 21  18:06","Sep 23  09:51","Dec 22  06:08"
"2008","Mar 20  05:49","Jun 21  00:00","Sep 22  15:45","Dec 21  12:04"
"2009","Mar 20  11:44","Jun 21  05:45","Sep 22  21:18","Dec 21  17:47"
"2010","Mar 20  17:32","Jun 21  11:28","Sep 23  03:09","Dec 21  23:38"
"2011","Mar 20  23:21","Jun 21  17:16","Sep 23  09:05","Dec 22  05:30"
"2012","Mar 20  05:15","Jun 20  23:08","Sep 22  14:49","Dec 21  11:12"
"2013","Mar 20  11:02","Jun 21  05:04","Sep 22  20:44","Dec 21  17:11"
"2014","Mar 20  16:57","Jun 21  10:52","Sep 23  02:30","Dec 21  23:03"
"2015","Mar 20  22:45","Jun 21  16:38","Sep 23  08:20","Dec 22  04:48"
"2016","Mar 20  04:31","Jun 20  22:35","Sep 22  14:21","Dec 21  10:45"
"2017","Mar 20  10:29","Jun 21  04:25","Sep 22  20:02","Dec 21  16:29"
"2018","Mar 20  16:15","Jun 21  10:07","Sep 23  01:54","Dec 21  22:22"
"2019","Mar 20  21:58","Jun 21  15:54","Sep 23  07:50","Dec 22  04:19"
"2020","Mar 20  03:50","Jun 20  21:43","Sep 22  13:31","Dec 21  10:03"
"2021","Mar 20  09:37","Jun 21  03:32","Sep 22  19:21","Dec 21  15:59"
"2022","Mar 20  15:33","Jun 21  09:14","Sep 23  01:04","Dec 21  21:48"
"2023","Mar 20  21:25","Jun 21  14:58","Sep 23  06:50","Dec 22  03:28"
"2024","Mar 20  03:07","Jun 20  20:51","Sep 22  12:44","Dec 21  09:20"
"2025","Mar 20  09:02","Jun 21  02:42","Sep 22  18:20","Dec 21  15:03"
"2026","Mar 20  14:46","Jun 21  08:25","Sep 23  00:06","Dec 21  20:50"
"2027","Mar 20  20:25","Jun 21  14:11","Sep 23  06:02","Dec 22  02:43"
"2028","Mar 20  02:17","Jun 20  20:02","Sep 22  11:45","Dec 21  08:20"
"2029","Mar 20  08:01","Jun 21  01:48","Sep 22  17:37","Dec 21  14:14"
"2030","Mar 20  13:51","Jun 21  07:31","Sep 22  23:27","Dec 21  20:09"
"2031","Mar 20  19:41","Jun 21  13:17","Sep 23  05:15","Dec 22  01:56"
"2032","Mar 20  01:23","Jun 20  19:09","Sep 22  11:11","Dec 21  07:57"
"2033","Mar 20  07:23","Jun 21  01:01","Sep 22  16:52","Dec 21  13:45"
"2034","Mar 20  13:18","Jun 21  06:45","Sep 22  22:41","Dec 21  19:35"
"2035","Mar 20  19:03","Jun 21  12:33","Sep 23  04:39","Dec 22  01:31"
"2036","Mar 20  01:02","Jun 20  18:31","Sep 22  10:23","Dec 21  07:12"
"2037","Mar 20  06:50","Jun 21  00:22","Sep 22  16:13","Dec 21  13:08"
"2038","Mar 20  12:40","Jun 21  06:09","Sep 22  22:02","Dec 21  19:01"
"2039","Mar 20  18:32","Jun 21  11:58","Sep 23  03:50","Dec 22  00:41"
"2040","Mar 20  00:11","Jun 20  17:46","Sep 22  09:44","Dec 21  06:33"
"2041","Mar 20  06:07","Jun 20  23:37","Sep 22  15:27","Dec 21  12:19"
"2042","Mar 20  11:53","Jun 21  05:16","Sep 22  21:11","Dec 21  18:04"
"2043","Mar 20  17:29","Jun 21  10:59","Sep 23  03:07","Dec 22  00:02"
"2044","Mar 19  23:20","Jun 20  16:50","Sep 22  08:47","Dec 21  05:43"
"2045","Mar 20  05:08","Jun 20  22:34","Sep 22  14:33","Dec 21  11:36"
"2046","Mar 20  10:58","Jun 21  04:15","Sep 22  20:22","Dec 21  17:28"
"2047","Mar 20  16:52","Jun 21  10:02","Sep 23  02:07","Dec 21  23:07"
"2048","Mar 19  22:34","Jun 20  15:54","Sep 22  08:01","Dec 21  05:02"
"2049","Mar 20  04:28","Jun 20  21:47","Sep 22  13:42","Dec 21  10:51"
"2050","Mar 20  10:20","Jun 21  03:33","Sep 22  19:29","Dec 21  16:39"
"2051","Mar 20  15:58","Jun 21  09:17","Sep 23  01:26","Dec 21  22:33"
"2052","Mar 19  21:56","Jun 20  15:16","Sep 22  07:16","Dec 21  04:18"
"2053","Mar 20  03:46","Jun 20  21:03","Sep 22  13:05","Dec 21  10:09"
"2054","Mar 20  09:35","Jun 21  02:47","Sep 22  19:00","Dec 21  16:10"
"2055","Mar 20  15:28","Jun 21  08:39","Sep 23  00:48","Dec 21  21:56"
"2056","Mar 19  21:11","Jun 20  14:29","Sep 22  06:40","Dec 21  03:52"
"2057","Mar 20  03:08","Jun 20  20:19","Sep 22  12:23","Dec 21  09:42"
"2058","Mar 20  09:04","Jun 21  02:03","Sep 22  18:07","Dec 21  15:24"
"2059","Mar 20  14:44","Jun 21  07:47","Sep 23  00:03","Dec 21  21:18"
"2060","Mar 19  20:37","Jun 20  13:44","Sep 22  05:47","Dec 21  03:00"
"2061","Mar 20  02:26","Jun 20  19:33","Sep 22  11:31","Dec 21  08:49"
"2062","Mar 20  08:07","Jun 21  01:10","Sep 22  17:19","Dec 21  14:42"
"2063","Mar 20  13:59","Jun 21  07:02","Sep 22  23:08","Dec 21  20:22"
"2064","Mar 19  19:40","Jun 20  12:47","Sep 22  04:58","Dec 21  02:10"
"2065","Mar 20  01:27","Jun 20  18:31","Sep 22  10:41","Dec 21  07:59"
"2066","Mar 20  07:19","Jun 21  00:16","Sep 22  16:27","Dec 21  13:45"
"2067","Mar 20  12:55","Jun 21  05:56","Sep 22  22:20","Dec 21  19:44"
"2068","Mar 19  18:51","Jun 20  11:55","Sep 22  04:09","Dec 21  01:34"
"2069","Mar 20  00:44","Jun 20  17:40","Sep 22  09:51","Dec 21  07:21"
"2070","Mar 20  06:35","Jun 20  23:22","Sep 22  15:45","Dec 21  13:19"
"2071","Mar 20  12:36","Jun 21  05:21","Sep 22  21:39","Dec 21  19:05"
"2072","Mar 19  18:19","Jun 20  11:12","Sep 22  03:26","Dec 21  00:54"
"2073","Mar 20  00:12","Jun 20  17:06","Sep 22  09:14","Dec 21  06:50"
"2074","Mar 20  06:09","Jun 20  22:59","Sep 22  15:04","Dec 21  12:36"
"2075","Mar 20  11:48","Jun 21  04:41","Sep 22  21:00","Dec 21  18:28"
"2076","Mar 19  17:37","Jun 20  10:35","Sep 22  02:48","Dec 21  00:12"
"2077","Mar 19  23:30","Jun 20  16:23","Sep 22  08:35","Dec 21  06:00"
"2078","Mar 20  05:11","Jun 20  21:58","Sep 22  14:25","Dec 21  11:59"
"2079","Mar 20  11:03","Jun 21  03:51","Sep 22  20:15","Dec 21  17:46"
"2080","Mar 19  16:43","Jun 20  09:33","Sep 22  01:55","Dec 20  23:31"
"2081","Mar 19  22:34","Jun 20  15:16","Sep 22  07:38","Dec 21  05:22"
"2082","Mar 20  04:32","Jun 20  21:04","Sep 22  13:24","Dec 21  11:06"
"2083","Mar 20  10:08","Jun 21  02:41","Sep 22  19:10","Dec 21  16:51"
"2084","Mar 19  15:58","Jun 20  08:39","Sep 22  00:58","Dec 20  22:40"
"2085","Mar 19  21:53","Jun 20  14:33","Sep 22  06:43","Dec 21  04:29"
"2086","Mar 20  03:36","Jun 20  20:11","Sep 22  12:33","Dec 21  10:24"
"2087","Mar 20  09:27","Jun 21  02:05","Sep 22  18:27","Dec 21  16:07"
"2088","Mar 19  15:16","Jun 20  07:57","Sep 22  00:18","Dec 20  21:56"
"2089","Mar 19  21:07","Jun 20  13:43","Sep 22  06:07","Dec 21  03:53"
"2090","Mar 20  03:03","Jun 20  19:37","Sep 22  12:01","Dec 21  09:45"
"2091","Mar 20  08:40","Jun 21  01:17","Sep 22  17:49","Dec 21  15:37"
"2092","Mar 19  14:33","Jun 20  07:14","Sep 21  23:41","Dec 20  21:31"
"2093","Mar 19  20:35","Jun 20  13:08","Sep 22  05:30","Dec 21  03:21"
"2094","Mar 20  02:20","Jun 20  18:40","Sep 22  11:15","Dec 21  09:11"
"2095","Mar 20  08:14","Jun 21  00:38","Sep 22  17:10","Dec 21  15:00"
"2096","Mar 19  14:03","Jun 20  06:31","Sep 21  22:55","Dec 20  20:46"
"2097","Mar 19  19:49","Jun 20  12:14","Sep 22  04:37","Dec 21  02:38"
"2098","Mar 20  01:38","Jun 20  18:01","Sep 22  10:22","Dec 21  08:19"
"2099","Mar 20  07:17","Jun 20  23:41","Sep 22  16:10","Dec 21  14:04"
"2100","Mar 20  13:04","Jun 21  05:32","Sep 22  22:00","Dec 21  19:51"

另存为 seasons.csv

现在使用这些代码:

$Seasons=Import-Csv path\to\seasons.csv | % 
$MEqui=[DateTime]$([string]($_.Year )+$_.marequi)
$JSols=[DateTime]$([string]($_.Year )+$_.junsols)
$SEqui=[DateTime]$([string]($_.Year )+$_.sepequi)
$DSols=[DateTime]$([string]($_.Year )+$_.decsols)
[pscustomobject]@mequi=$mequi;jsols=$jsols;sequi=$sequi;dsols=$dsols

$Seasons | %
$MEqui=$MEqui.ToString(yyyy-MM-ddTHH:mm:ssZ)
$JSols=$JSols.ToString(yyyy-MM-ddTHH:mm:ssZ)
$SEqui=$SEqui.ToString(yyyy-MM-ddTHH:mm:ssZ)
$DSols=$DSols.ToString(yyyy-MM-ddTHH:mm:ssZ)
[pscustomobject]@MarchEquinox=$mequi;JuneSolstice=$jsols;SeptemberEquinox=$sequi;DecemberSolstice=$dsols | Export-Csv path\to\output.csv -NoTypeInformation -Append

其余步骤相同(只需将 $timetable1 替换为 $seasons),这种方法更好,因为从 csv 导入和导出到 csv 更容易且更可靠。

【问题讨论】:

ConvertFrom-String 提供基于分隔符的解析以及基于包含示例值的模板的启发式解析。基于分隔符的解析应用了您无法控制的自动类型转换,并且模板语言的文档记录很差,确切的行为难以预测 - 最好完全避免使用此 cmdlet。另请注意,它在 PowerShell [Core] v6+ 中不可用。 【参考方案1】:

对于模板问题,junsols 列似乎只有一个示例值 (Jun 21),如果您更新它,您会发现它工作得很好。 一般来说,我会推荐以下模板来涵盖所有日期:

[int]year*:0000   [string]marequi:Mar 00  00:00   [string]junsols:Jun 00  00:00   [string]sepequi:Sep 00  00:00   [string]decsols:Dec 00  00:00
[int]year*:9999   [string]marequi:Mar 99  99:99   [string]junsols:Jun 99  99:99   [string]sepequi:Sep 99  99:99   [string]decsols:Dec 99  99:99

另外(作为旁注):try to avoid using the increase assignment operator (+=) to create a collection 因为它是指数级的昂贵

【讨论】:

太棒了!你真的解决了这个问题!我已经确认它正在工作,我认为这可能是一个错误......说实话,我遵循了 Convertfrom-String 的 Microsoft 文档页面上的示例,它实际上并没有说明如何编写模板......

以上是关于PowerShell - 一个与 convertfrom-string 相关的奇怪问题的主要内容,如果未能解决你的问题,请参考以下文章

PowerShell命令与脚本(10)——脚本

PowerShell - 一个与 convertfrom-string 相关的奇怪问题

powershell Powershell功能与ConEmu一起使用。选项卡功能在当前位置打开一个新选项卡。 Pane打开了另一个控制台窗口

powershell Powershell用于将Powershell与SharePoint 2007一起使用

PowerShell 脚本速度独立与 Visual Code 内

powershell WPF与PowerShell