在Array PowerShell中获得新添加的价值

发布于 2025-01-18 19:23:44 字数 580 浏览 0 评论 0 原文

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   

}

它打印所有名称,而不是一个新项目

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   

}

It prints all the names instead of the one new item

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红尘作伴 2025-01-25 19:23:44

tl; dr

用以下呼叫 foreach 循环。 /compare-object“ rel =” nofollow noreferrer“> compare-object

# Compare the new and the old collection items by their .Name property
# and output the name of those that are unique to the new collection.
Compare-Object -Property Name $FolderItems $oldFolderItems |
  Where-Object SideIndicator -eq '<=' |
    ForEach-Object Name

您还应初始化 $ oldfolderitems to $ null $ null $ oldcount 0 要安全,除非您想要在第一次迭代中输出 all> all> All danclosing <代码>如果对:
语句
if($ oldFolderItems -and $ folderitems.count -gt $ oldcount){#...

注意:立即 - 但效率低下 - fix fix to your tob to以下是下一节中解释的原因:

if ($oldFolderItems.Name -contains $item.Name) { # Compare by .Name values

注意: $ 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:

# !! Returns $false, because the two [System.IO.DirectoryInfo]
# !! instances are distinct objects.
@(Get-Item /) -contains (Get-Item /)

Therefore, instances of .NET reference types must be compared by the value of an identifying property(如果有的话,例如 .name 在这种情况下)而不是整体

。 。 eg:

(Get-Item /).GetType().IsValueType # -> $false -> reference type

# Equivalent, with a type literal
[System.IO.DirectoryInfo].IsValueType # -> $false

[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 to Compare-Object:

# Compare the new and the old collection items by their .Name property
# and output the name of those that are unique to the new collection.
Compare-Object -Property Name $FolderItems $oldFolderItems |
  Where-Object SideIndicator -eq '<=' |
    ForEach-Object Name

You should also initialize $oldFolderItems to $null and $oldCount to 0, to be safe, and - unless you want all names to be output in the first iteration - change the enclosing if 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:

if ($oldFolderItems.Name -contains $item.Name) { # Compare by .Name values

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 by Get-Item:

# !! Returns $false, because the two [System.IO.DirectoryInfo]
# !! instances are distinct objects.
@(Get-Item /) -contains (Get-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.:

(Get-Item /).GetType().IsValueType # -> $false -> reference type

# Equivalent, with a type literal
[System.IO.DirectoryInfo].IsValueType # -> $false

[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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文