List 从多个值筛选列表
我有一句话:List 从多个值筛选列表,list,powershell,filtering,List,Powershell,Filtering,我有一句话: $output.GetEnumerator() | Where-Object { $_.ItemPath -notlike "*.zip" } | Select ItemPath 示例输出: ItemPath -------- \\devws04
$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
还有其他一些片段,但都不起作用。这对结果毫无影响。任何暗示都感谢
这是完整的输出:
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
是什么
但使用变量的工作原理应该完全相同:
$ext = "*.zip"
$output | Where-Object { $_.ItemPath -notlike $ext } | Select ItemPath
您还可以使用-notin表示一系列内容,具体取决于输入:
gci | Where-Object { $_.Extension -notin ($ext, ".7z") } | Select FullName
不确定
$output
是什么
但使用变量的工作原理应该完全相同:
$ext = "*.zip"
$output | Where-Object { $_.ItemPath -notlike $ext } | Select ItemPath
您还可以使用-notin表示一系列内容,具体取决于输入:
gci | Where-Object { $_.Extension -notin ($ext, ".7z") } | Select FullName
您还可以使用正则表达式帮助您应对所面临的挑战:
$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!
您还可以使用正则表达式帮助您应对所面临的挑战:
$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!
$ouput只是一个[PSCustomObject],一个包含具有简单属性和属性的对象的列表。你的代码片段不起作用。我需要多个排除项,而不仅仅是“.zip”。@eMi不要在各种注释中添加额外的信息,您的问题是要包含这些信息。@eMi我需要创建对象的方法,然后,如果它是一个单独的属性,您要与一组对象进行比较,那么我们需要做一些不同的事情。
$\uu0.ItemPath-不喜欢“*$ext”
(注意通配符*
)。@JosefZ yep!虽然在变量中可能会更好。#更新的$output只是一个[PSCustomObject],一个包含简单属性和属性的对象的列表。您的代码段不起作用。我需要多个排除项,而不仅仅是“.zip”“@eMi不要在各种注释中添加其他信息,您的问题需要包含这些信息。@eMi我需要创建对象的方法,然后,如果它是一个单独的属性,您希望与对象数组进行比较,那么我们需要做一些不同的事情。$\uuu.ItemPath-与“*$ext”不同。
(注意通配符*
).@JosefZ yep!虽然在变量中可能会更好。#updated什么是$output?您希望返回路径还是完整对象?是的,它将是完整对象..请查看我的更新answer@M好了,非常感谢!请考虑把最终结果分享给其他人,让他们从你的问题中学到什么?您想要返回路径还是完整对象?是的,它将是完整对象。请查看我的更新answer@M那很好,非常感谢!请考虑将最终结果分享给其他人以学习您的问题:)谢谢,这很好,但是我正在寻找一个没有正则表达式的解决方案,并且忽略值的定义可以保持这样:$iGiOrgFixs= @(“.zip”,“.PSD”)您能详细说明不想使用正则表达式的理由吗?酷-很高兴我能帮忙。如果你担心其他人稍后会在你的脚本中填写细节,那么你可以从字符串/扩展数组构建你的正则表达式。是的,这是我的想法:)我还有一个问题,我如何返回“object”本身而不是属性“ItemPath”?我尝试过:$results=$output.GetEnumerator()| Where Object{($\.ItemPath-notmatch$regex)}如果$output是[PSCustomObject]的结果而不是哈希表,则不需要.GetEnumerator()方法。使用$path=($output | Where Object ItemPath-notmatch$regex)。ItemPath
作为快捷方式,而不是选择Object-ExpandProperty…
谢谢,这很好,但我正在寻找一个没有regex的解决方案,并且忽略值的定义可以保持这样:$ignoreFiles=@(“.zip”,“.psd”)你能详细解释一下不想使用正则表达式的原因吗?很酷-很高兴我能帮上忙。如果你担心其他人稍后会在你的脚本中填写细节,那么你可以从字符串/扩展数组构建你的正则表达式。是的,这是我的想法:)我还有一个问题,我如何返回“object”本身而不是属性“ItemPath”?我尝试过:$results=$output.GetEnumerator()| Where Object{($\.ItemPath-notmatch$regex)}如果$output是[PSCustomObject]的结果而不是哈希表,则不需要.GetEnumerator()方法。使用$path=($output | Where Object ItemPath-notmatch$regex)。ItemPath
作为快捷方式,而不是选择对象-ExpandProperty…