在Array PowerShell中获得新添加的价值
for (;;) {
#Get All Files from the Folder
$FolderItems = @(Get-PnPFolderItem -FolderSiteRelativeUrl $FolderURL -ItemType File)
Write-Host "Total Number of Files in the Folder:" $FolderItems.Count
if ($FolderItems.Count -gt $oldCount) {
foreach ($item in $FolderItems) {
if ($oldFolderItems -contains $item) {
}
else {
Write-Host $item.Name
}
}
}
$oldCount = $FolderItems.Count
$oldFolderItems = $FolderItems
timeout 180
}
它打印所有名称,而不是一个新项目
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tl; dr
用以下呼叫 foreach 循环。 /compare-object“ rel =” nofollow noreferrer“>
compare-object
:您还应初始化
$ oldfolderitems
to$ null
$ null$ oldcount
0
要安全,除非您想要在第一次迭代中输出 all> all> All danclosing <代码>如果对:语句
if($ oldFolderItems -and $ folderitems.count -gt $ oldcount){#...
注意:立即 - 但效率低下 - fix fix to your tob to以下是下一节中解释的原因:
注意:
$ oldfolderitems.name
实际上返回 array.name
elements in Collection$ oldFolderItems
,这是一个方便的功能,名为 成员-Access枚举 。。 strong>:
It's unclear what .NET type
Get-PnPFolderItem
returns instances of, but it's fair to assume that the type is a .NET除非明确设计参考类型以基于识别属性比较其实例,否则 [1] 参考equality equality 在基于等值的测试中的 测试诸如
-contains
(但在其他等式 - 比较操作中,例如-in
and 和-eq
),IE <<强>对的两个引用 相等。因此,在您的情况下,使用
-contains
无法正常工作,因为收藏的元素(即使它们概念上表示相同的对象)是 dintesp 不同的实例< /em>将其比较为 不等式。一个简化的示例,使用 /code> instances, as output by
Get-Item
:Therefore, instances of .NET reference types must be compared by the value of an identifying property(如果有的话,例如
.name
在这种情况下)而不是整体。 。 eg:
[1] A notable example is the
[string]
type, which, as an exception, generally behaves like a value type, so that the following is尽管涉及技术不同的实例,但仍然$ true
:$ s1 ='foo'; $ s2 ='f' +'oo'; $ S1 -EQ $ S2
tl;dr
Replace your
foreach
loop with the following call toCompare-Object
:You should also initialize
$oldFolderItems
to$null
and$oldCount
to0
, to be safe, and - unless you want all names to be output in the first iteration - change the enclosingif
statement to:if ($oldFolderItems -and $FolderItems.Count -gt $oldCount) { # ...
Note: The immediate - but inefficient - fix to your attempt would have been the following, for the reasons explained in the next section:
Note:
$oldFolderItems.Name
actually returns the array of.Name
property values of the elements in collection$oldFolderItems
, which is a convenient feature named member-access enumeration.As for what you tried:
It's unclear what .NET type
Get-PnPFolderItem
returns instances of, but it's fair to assume that the type is a .NET reference type (as opposed to a value type).Unless a reference type is explicitly designed to compare its instances based on identifying properties,[1] reference equality is tested for in equality test-based operations such as
-contains
(but also in other equality-comparison operations, such as with-in
and-eq
), i.e. only two references to the very same instance are considered equal.Therefore, using
-contains
in your case won't work, because the elements of the collections - even if they conceptually represent the same objects - are distinct instances that compare as unequal.A simplified example, using
System.IO.DirectoryInfo
instances, as output byGet-Item
:Therefore, instances of .NET reference types must be compared by the value of an identifying property (if available, such as
.Name
in this case) rather than as a whole.To discover whether a given instance is one of a .NET reference type, access the type's
.IsValueType
property: a return value of$false
indicates a reference type; e.g.:[1] A notable example is the
[string]
type, which, as an exception, generally behaves like a value type, so that the following is still$true
, despite technically distinct instances being involved:$s1 = 'foo'; $s2 = 'f' + 'oo'; $s1 -eq $s2