$ c.length仅输出一吨而不是长度

问题描述 投票:1回答:1

因此,我一直在尝试学习一些Powershell,并在尝试制作以下脚本(从Get-Process获取所有Starttime值并将其保存为DateTime对象的Time部分之外的形式。)

    $a=( Get-Process |Where-Object ProcessName -NotMatch 'Idle' | Select-Object -property starttime);

 #I think Select-Object part may not be necessary, but left in as that's how 
 #I've coded it

 $b= $a.starttime; 

$c=New-Object System.Collections.Generic.List[DateTime];

$count=0; @(0..($b.length-1)).foreach({ Write-Host $count,$b[$_].ToShortDateString(); $c.add($b[$_].ToShortDateString()); $count++ }) 

并且我认为它按照我的意图进行。调用$b[j]-$c[j]仅产生分钟,秒,毫秒的值。但是,当我调用长度时,得到以下内容

$b.length

175

$c.length

1

1

1

等,

那为什么呢?我不太了解powershell(玩了大约一周),并且觉得$c.length应该吐出与$b.length相同的值吗?我做错了什么?

powershell loops introduction
1个回答
1
投票

首先,解决此问题的方法很简单-使用Count获取列表中项目的总数:

© www.soinside.com 2019 - 2024. All rights reserved.