【问题标题】:Filter list from multiple values从多个值中过滤列表
【发布时间】:2018-12-07 04:57:14
【问题描述】:

我有这行:

$output.GetEnumerator() | Where-Object { $_.ItemPath -notlike "*.zip" } | Select ItemPath

示例输出:

ItemPath                                                    
--------                                                    
\\devws04                                                   
\\devws04\c$                                                
\\devws04\c$\share                                          
\\devws04\c$\share\Dieser ORdner ist offline erstellt worden
\\devws04\c$\share\FileOfflineMode - Copy.txt               
\\devws04\c$\share\FileOfflineMode.txt                      
\\devws04\c$\share\Personal                                 
\\devws04\c$\share\Personal\Okey Cuuus.txt                  
\\devws04\c$\share\Portfolio.txt   

该行不包括包含“*.zip”的路径值。它可以工作,但我想在这个地方使用一个变量,而不是用“and”将所有内容附加到同一行。

我尝试了以下操作:

$ignoreFiles = @("*.zip", "*.psd")
$output.GetEnumerator() | Where-Object { $_.ItemPath -notin $ignoreFiles } | Select ItemPath

还有其他几个 sn-ps,但没有一个起作用。它对结果没有任何作用。任何提示表示赞赏!

这是完整的输出:

ItemPath                                                                            
--------                                                                            
\\devws04                                                                           
\\devws04\c$                                                                        
\\devws04\c$\share                                                                  
\\devws04\c$\share\Dieser ORdner ist offline erstellt worden                        
\\devws04\c$\share\Dieser ORdner ist offline erstellt worden\MeineofflineDateien.zip
\\devws04\c$\share\FileOfflineMode - Copy.txt                                       
\\devws04\c$\share\FileOfflineMode.txt                                              
\\devws04\c$\share\Personal                                                         
\\devws04\c$\share\Personal\Okey Cuuus.txt                                          
\\devws04\c$\share\Portfolio.txt 

编辑: 这是实际代码,我的目标是进行 csv 导出,但在我想过滤上述值之前:

$ignoreFiles = "\.zip|\.psd"
#$ignoreFiles = @("*.zip", "*.psd")
    $query = Get-WmiObject -query "SELECT * FROM Win32_OfflineFilesItem"
    $hostName = [System.Net.Dns]::GetHostName()
    $user = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
    $output = @()
    $query | ? {
    $output += [PSCustomObject] @{
            User = $user
            Hostname = $hostName
            ItemPath = $_.ItemPath 
        }
    } 

    $output  | Export-CSV $outputPath -NoTypeInformation # ONLY EXPORT OBJECTS WHERE $_.ItemPath does not contain one of the values from $ignoreFiles!

【问题讨论】:

  • 什么是 $output?您想要只返回路径还是完整对象?
  • 是的,它会是完整的对象..
  • 请看我更新的答案
  • @Mötz 很好,非常感谢!
  • 请考虑分享最终结果以供其他人从您的问题中学习:)

标签: list powershell filtering


【解决方案1】:

您还可以使用正则表达式来帮助您应对所面临的挑战:

$regex = "\.zip|\.psd"
$paths = $output.GetEnumerator() | Select-Object ItemPath

$paths.ItemPath -notmatch $regex

更新了额外的答案

我会说这应该可以解决问题

$output = $output | Where-Object {$_.ItemPath -notmatch $ignoreFiles }
$output  | Export-CSV $outputPath -NoTypeInformation # ONLY EXPORT OBJECTS WHERE $_.ItemPath does not contain one of the values from $ignoreFiles!

【讨论】:

  • 谢谢,这很好用,但我正在寻找一个没有正则表达式的解决方案,并且忽略值的定义可以保持这样:$ignoreFiles = @(".zip", ".psd")
  • 您能否详细说明不想想要使用正则表达式的原因?
  • 酷 - 很高兴我能帮上忙。如果您担心其他人稍后在您的脚本中填写详细信息,您可以从一组字符串/扩展名构建您的正则表达式。
  • 是的,这是我的想法 :) 我还有一个问题,我将如何返回“对象”本身而不是属性“ItemPath”?我试过: $results = $output.GetEnumerator() | Where-Object { ($_.ItemPath -notmatch $regex) }
  • 如果 $output 是 [PSCustomObject] 而不是哈希表的结果,则不需要 .GetEnumerator() 方法。使用$paths = ($output | Where-Object ItemPath -notmatch $regex).ItemPath 作为快捷方式而不是Select-Object -ExpandProperty ...
【解决方案2】:

不确定$output 是什么,

但是使用变量应该完全一样:

$ext = "*.zip"
$output | Where-Object { $_.ItemPath -notlike $ext } | Select ItemPath

您还可以将 -notin 用于一系列事物,具体取决于输入:

gci | Where-Object { $_.Extension -notin ($ext, ".7z") } | Select FullName

【讨论】:

  • $ouput 只是一个 [PSCustomObject],一个包含具有简单属性和属性的对象的列表。你的 sn-ps 不工作。我想要多个排除项,而不仅仅是“.zip”。
  • @eMi 不要在各种 cmets 中添加其他信息,edit您的问题包含该信息。
  • @eMi 我需要创建对象的方法然后,如果它是您想要与对象数组比较的单个属性,那么我们会想做一些不同的事情。
  • $_.ItemPath -notlike "*$ext"(注意通配符*)。
  • @JosefZ 是的!尽管在变量中可能会更好。 #更新
猜你喜欢
  • 1970-01-01
  • 2018-01-25
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 2020-01-20
  • 2019-01-28
  • 1970-01-01
  • 2022-01-22
相关资源
最近更新 更多