相关疑难解决方法(0)

powershell在什么条件下展开管道中的项目?

考虑以下:

function OutputArray{
    $l = @(,(10,20))
    $l
}

(OutputArray) -is [collections.ienumerable]
# C:\ PS> True
(OutputArray).Count
# C:\ PS> 2
Run Code Online (Sandbox Code Playgroud)

$l 当它进入管道时被"展开". 这个答案表明powershell展开了所有集合. 哈希表是一个集合.但是,哈希表当然不受管道的影响:

function OutputHashtable{
    $h = @{nested=@{prop1=10;prop2=20}}
    $h
}

(OutputHashtable) -is [collections.ienumerable]
# C:\ PS> True
(OutputHashtable).Count
# C:\ PS> 1
Run Code Online (Sandbox Code Playgroud)

这条评论表明,所有IEnumerable都转换为对象数组.但是,数组和散列表都是不可数的:

@(,(10,20)) -is [collections.ienumerable]
#True
@{nested=@{prop1=10;prop2=20}} -is [collections.ienumerable]
#True
Run Code Online (Sandbox Code Playgroud)

究竟,PowerShell将对象"展开"到管道中的条件是什么?

collections powershell ienumerable pipeline

13
推荐指数
1
解决办法
1638
查看次数

为什么创建数组时需要使用前导逗号?

我想创建一个包含两个数字的数组的数组.非常直截了当.但是,如果我在第一个数组之前没有提供前导逗号,则它是不正确的.为什么需要这个领先的逗号?

PS C:\src\powershell> Get-Content .\fr-btest.ps1
$files1 = @(
@(4, 1024)
, @((7), (16))
)

$files1
$files1.GetType()
$files1.Length
$files1.Count
'========'

$files2 = @(
, @(4, 1024)
, @((7), (16))
)

$files2
$files2.GetType()
$files2.Length
$files2.Count

PS C:\src\powershell> .\fr-btest.ps1
4
1024
7
16

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
3
3
========
4
1024
7
16
True     True     Object[]                                 System.Array
2
2
Run Code Online (Sandbox Code Playgroud)

arrays powershell

8
推荐指数
2
解决办法
926
查看次数

没有'.Count'属性的对象 - 使用@()(数组子表达式运算符)与[Array]强制转换

我试图执行一些简单的if语句,但所有基于[Microsoft.Management.Infrastructure.CimInstance]的新cmdlet似乎都没有公开.count方法?

$Disks = Get-Disk
$Disks.Count
Run Code Online (Sandbox Code Playgroud)

不归还任何东西.我发现我可以将它转换为[数组],这使得它返回一个.NET .count方法,如预期的那样.

[Array]$Disks = Get-Disk
$Disks.Count
Run Code Online (Sandbox Code Playgroud)

这可以直接将其作为以前cmdlet的数组投射:

(Get-Services).Count
Run Code Online (Sandbox Code Playgroud)

推荐的解决方法是什么?

一个不起作用的例子:

$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
  If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
   Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
   Else If ($PageDisk.Count -eq 1) { Do X }
Run Code Online (Sandbox Code Playgroud)

选项A(演员阵容):

[Array]$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
  If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue} …
Run Code Online (Sandbox Code Playgroud)

arrays powershell cmdlets scalar cim

6
推荐指数
1
解决办法
804
查看次数

为什么PowerShell会自动展平数组?

我写了一些pwsh代码

"a:b;c:d;e:f".Split(";") | ForEach-Object { $_.Split(":") }
# => @(a, b, c, d, e, f)
Run Code Online (Sandbox Code Playgroud)

但是我想要这个

// in javascript
"a:b;c:d;e:f".split(";").map(str => str.split(":"))
[ [ 'a', 'b' ], [ 'c', 'd' ], [ 'e', 'f' ] ]
Run Code Online (Sandbox Code Playgroud)

嵌套数组

@(
    @(a, b),
    @(c, d),
    @(e, f),
)
Run Code Online (Sandbox Code Playgroud)

为什么?那我该怎么办

arrays powershell pipeline jagged-arrays

4
推荐指数
1
解决办法
92
查看次数