解释 Powershell [void] 行为 [英] Explain Powershell [void] behavior
问题描述
我的脚本将一些信息从 Sharepoint 站点导出到 .CSV
文件.
My script exports some information from Sharepoint sites to a .CSV
file.
# Enter Web Application URL
$WebAppURL="http://server"
# Enter Path to CSV-file
$CSVFilePath="Path"
$SiteCollections = Get-SPWebApplication $WebAppURL | Get-SPSite -Limit All
$Tree = foreach ($Site in $SiteCollections) {
foreach ($Web in $Site.AllWebs) {
$Groups = New-Object System.Collections.ArrayList
foreach ($Group in $web.Groups) {
[void]$Groups.Add($Group.Name)
}
$Groups = $Groups -join ','
[PSCustomObject]@{
Url = $web.URL
Title = $web.Title
CountOfLists= $web.Lists.Count
PermissionsInherited = $web.Permissions.Inherited
Groups = $Groups
}
}
}
$Tree | Export-CSV $CSVFilePath -NoTypeInformation -Encoding UTF8
如果我在 $Groups.Add($Group.Name)
之前使用 [void]
它可以正常工作,但没有 [void]
,脚本返回一个空的 .CSV
文件.
If I use [void]
before $Groups.Add($Group.Name)
it works correctly, but without [void]
, the script returns an empty .CSV
file.
为什么会发生这种情况?
Why does this happen?
推荐答案
PowerShell 隐式输出既没有在变量中捕获的值,也没有被抑制(如
[void] (...)
,$null= ...
或Out-Null
),重定向(使用>
/>>>
),也不发送通过管道 (|
) 到另一个命令.PowerShell implicitly outputs values that are neither captured in a variable, suppressed (as with
[void] (...)
,$null = ...
, orOut-Null
), redirected (with>
/>>
), nor sent through the pipeline (|
) to another command.- 有关此行为背后的设计原理,请参阅此答案的底部部分.
- See the bottom section of this answer for the design rationale behind this behavior.
$Groups.Add($Group.Name)
输出一个[int]
(System.Int32
) 值(新附加元素的索引),所以没有[void]
强制转换这个整数首先成为内部然后外部foreach
循环的输出[1] 的一部分,从而有效地成为第一个输出对象在$Tree
中捕获.$Groups.Add($Group.Name)
outputs an[int]
(System.Int32
) value (the index of the newly appended element), so without the[void]
cast this integer becomes part of first the inner and then the outerforeach
loop's output[1], and thereby effectively the very first output object captured in$Tree
.当管道对象到
导出时-Csv
,它是 第一个 对象,它根据该对象的(公共)属性确定要导出的列.When piping objects to
Export-Csv
, it is the first object alone that determines what columns to export, based on that object's (public) properties.[int]
实例本身只是一个值,它没有任何属性,因此生成的 CSV 文件实际上是空的.>
An
[int]
instance is just a value itself, it doesn't have any properties, so the resulting CSV file is effectively empty.[1]
System.Collections.ArrayList
的通用等效项是System.Collections.Generic.List`1
,其.Add()
方法不不返回一个值,这就是为什么它在PowerShell中(和一般情况下)使用更可取的原因.等效的构造调用将是:$Groups = New-Object System.Collections.Generic.List[object]
或者,在 PSv5+ 中,$Groups = [System.Collections.Generic.List[object]]::new()
[1] The generic equivalent of
System.Collections.ArrayList
isSystem.Collections.Generic.List`1
, whose.Add()
method does not return a value, which is why its use is preferable in PowerShell (and generally). The equivalent construct call would be:
$Groups = New-Object System.Collections.Generic.List[object]
or, in PSv5+,
$Groups = [System.Collections.Generic.List[object]]::new()
这篇关于解释 Powershell [void] 行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!