【发布时间】:2019-10-02 14:55:56
【问题描述】:
我一直在尝试学习一些 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] 只会产生 Minutes、Seconds、Milliseconds 的值。但是,当我调用长度时,我得到以下内容
$b.length
175
$c.length
1
1
1
等等,
那为什么会这样呢?我对powershell不太精通(玩了大约一个星期),我觉得$c.length应该吐出与$b.length相同的值?我做错了什么?
【问题讨论】:
-
System.Collections.Generic.List`1类型没有Length属性。请改用Count -
哎呀,我想如果是这样的话它会抛出一个错误,我什至没有注意到。有没有办法仔细检查某些东西有什么属性?当我在 $b 上尝试 Get-member 并且那个也是 DateTime 对象时,是因为它在技术上不是 System.Collections.Generic.List 吗?还是有其他原因?
-
管道默认解开可枚举的对象,所以你想做
Get-Member -InputObject $c,而不是$c |Get-Member:) -
啊,是的,这是有道理的。 Pipe 一个接一个地发送东西,我知道(虽然显然我不太了解 xD)。谢谢!
标签: powershell