问题描述
Get-ADuser
cmdlet的示例:
An example of the Get-ADuser
cmdlet:
$Users = Get-ADuser -Filter *
在大多数情况下,它将返回多个ADuser对象,但是它是什么集合"类型?该文档仅说它将返回Microsoft.ActiveDirectory.Management.ADUser的一个或多个用户对象.
It will in most cases return multiple ADuser objects, but what "collection" type is it?The documentation only says it will returns one or more user objects of Microsoft.ActiveDirectory.Management.ADUser.
尝试使用例如($Users -is [System.Collections.ArrayList])
但我无法确定收藏"类型吗?
Tried to use e.g. ($Users -is [System.Collections.ArrayList])
but I cannot nail the "collection" type?
推荐答案
Cmdlet 自身通常在其输出中使用 no 收集类型.:
它们向管道发出单个对象,这些对象可以地表示:0
(无),1
或多个.
Cmdlets themselves typically use no collection type in their output.:
They emit individual objects to the pipeline, which can situationally mean: 0
(none), 1
, or multiple ones.
这正是Get-ADUser
的作用:输出对象的具体数量取决于给定的参数;这就是为什么仅 Get-AdUser
帮助主题的原因提到标量类型 ADUser
输出类型并声明其返回一个或多个".其中
This is precisely what Get-ADUser
does: the specific number of output objects depends on the arguments that were given; that is why the Get-AdUser
help topic only mentions scalar type ADUser
as the output type and states that it "returns one or more" of them.
通常,PowerShell管道应是对象流,其特定长度(对象计数)不需要事先知道,并在后续管道段中添加命令在收到前一个段的输出对象 时对其进行处理(请参阅 about_Pipelines ).
Generally, the PowerShell pipeline is meant to be a stream of objects whose particular length (object count) need not be known in advance, with commands in subsequent pipeline segments processing the previous segment's output objects one by one, as they're being received (see about_Pipelines).
但是, PowerShell 引擎会自动在[object[]]
数组中为您收集多个输出 (如果需要) ,特别是如果您通过变量赋值捕获输出或通过(...)
使用命令调用,则分组运算符(或$(...)
,即子表达式运算符 ),作为表达式:
However, it is the PowerShell engine that automatically collects multiple outputs for you in an [object[]]
array if needed, notably if you capture output via a variable assignment or use a command call via (...)
, the grouping operator (or $(...)
, the subexpression operator), as an expression:
# Get-ChildItem C:\Windows has *multiple* outputs, so PowerShell
# collects them in an [object[]] array.
PS> $var = Get-ChildItem C:\Windows; $var.GetType().Name
Object[]
# Ditto with (...) (and also with $(...) and always with @(...))
PS> (Get-ChildItem C:\Windows).GetType().Name
Object[]
但是,如果给定命令(可能是情境)仅输出一个单个对象,那么您将得到该对象本身 -不包裹在一个数组中:
However, if a given command - possibly situationally - only outputs a single object, you'll then get just that object itself - it is not wrapped in an array:
# Get-Item C:\ (always) returns just 1 object.
PS> $var = Get-Item C:\; $var.GetType().Name
DirectoryInfo # *not* a single-element array,
# just the System.IO.DirectoryInfo instance itself
棘手的是,给定的命令可以根据情况和输入条件地产生一个或多个输出,因此引擎可以返回单个对象或数组.
What can get tricky is that a given command can situationally produce either one or multiple outputs, depending on inputs and runtime conditions, so the engine may return either a single object or an array.
# !! What $var receives depends on the count of subdirs. in $HOME\Projects:
PS> $var = Get-ChildItem -Directory $HOME\Documents; $var.GetType().Name
??? # If there are *2 or more* subdirs: an Object[] array of DirectoryInfo instances.
# If there is only *one* subdir.: a DirectoryInfo instance itself.
# (See below for the case when there is *no* output.)
@()
,数组子表达式运算符,旨在消除这种歧义,如果需要的话:通过将命令包装在@(...)
中,PowerShell确保将其输出始终 收集为[object[]]
-即使该命令恰好产生一个 输出对象甚至 none :
@()
, the array-subexpression operator, is designed to eliminate this ambiguity, if needed:By wrapping a command in @(...)
, PowerShell ensures that its output is always collected as [object[]]
- even if the command happens to produce just one output object or even none:
PS> $var = @(Get-ChildItem -Directory $HOME\Projects); $var.GetType().Name
Object[] # Thanks to @(), the output is now *always* an [object[]] array.
对于变量分配,一种可能更有效的替代方法是使用[array]
类型约束 以确保输出变为数组:
With variable assignments, a potentially more efficient alternative is to use an [array]
type constraint to ensure that the output becomes an array:
# Alternative to @(...)
PS> [array] $var = Get-ChildItem -Directory $HOME\Documents; $var.GetType().Name
Object[]
注意:
-
这可能会更有效,因为如果RHS已经恰好是一个数组,则按原样分配它,而
@(...)
实际上枚举来自...
和然后将元素重新组合成一个 new ([object[]]
)数组.
This is potentially more efficient in that if the RHS already happens to be an array, it is assigned as-is, whereas
@(...)
actually enumerates the output from...
and then reassembles the elements into a new ([object[]]
) array.
-
[array]
通过简单地传递保留输入数组的特定类型(例如,[array] $var = [int[]] (1..3)
将[int[]]
数组按原样存储在$var
).
[array]
preserves the specific type of an input array by simply passing it through (e.g.,[array] $var = [int[]] (1..3)
stores the[int[]]
array as-is in$var
).
放置[array]
"cast"到$var = ...
的 left 的位置-这就是使它成为变量的 type约束的原因-意味着变量的类型已被锁定,并在以后为$var
分配不同的值将继续将RHS值转换为[array]
([object[]]
)(除非您分配$null
或"nothing"(请参阅下文))./p>
Placing the [array]
"cast" to the left of $var = ...
- which is what it makes it a type constraint on the variable - means that the type of the variable is locked in, and assigning different values to $var
later will continue to convert the RHS value to [array]
([object[]]
), if needed (unless you assign $null
or "nothing" (see below)).
如果命令没有产生 no 输出,您将得到"nothing" (严格来说: [System.Management.Automation.Internal.AutomationNull]::Value
单例),在大多数情况下的行为类似于$null
:
If a command produces no output, you'll get "nothing" (strictly speaking: the [System.Management.Automation.Internal.AutomationNull]::Value
singleton), which in most cases behaves like $null
:
# Get-Item nomatchingfiles* produces *no* output.
PS> $null -eq (Get-Item nomatchingfiles*)
True
# Conveniently, PowerShell lets you call .Count on this value, which the
# behaves like an empty collection and indicates 0.
PS> (Get-Item nomatchingfiles*).Count
0
这篇关于一个返回多个对象的cmdlet,它是什么集合类型(如果有)? [电源外壳]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!