为什么 powershell 将我的散列 table 转换为字符串块
Why the powershell convert my hash table into string block
我有一个哈希表 ($applications
),看起来像这样:
Name Version
---- -------
Adobe Flash Player 19 ActiveX 19.0.0.226
Enterprise Library 4.0 - May 2008 4.0.0.0
Entity Framework Designer for Visual Studio 2012 - enu 11.1.21009.00
Fiddler 2.4.0.6
Google Chrome 49.0.2623.87
IIS 8.0 Express 8.0.1557
所以我试图从列表中排除一些应用程序,为此我正在使用:
[array]$excludeApps = "Fiddler","Chrome"
foreach ($excludeApp in $excludeApps){
$applications = $applications -notlike "*$excludeApp*"
}
结果可能过滤掉了排除列表,但与我预期的不同:
@{Name=Adobe Flash Player 19 ActiveX; Version=19.0.0.226}
@{Name=Enterprise Library 4.0 - May 2008; Version=4.0.0.0}
@{Name=Entity Framework Designer for Visual Studio 2012 - enu; Version=11.1.21009.00}
@{Name=IIS 8.0 Express; Version=8.0.1557}
我尝试使用 GetEnumerator()
和 ${applications.Name}
的几种语法来处理这些值,但没有任何效果。我相信 PowerShell 将此列表检测为字符串。
知道如何处理吗?
将 select-对象与 -match/-notmatch
运算符一起使用。
即:
$application = $application | ? { $_.name -notmatch "fiddler" -or $_.name -notmatch "Chrome"}
或在循环中:
[array]$excludeApps = "Fiddler","Chrome"
foreach ($excludeApp in $excludeApps){
$applications = $applications | ? {$_.name -notmatch $excludeApp }
}
一方面,您的数据不是哈希表,而是自定义对象列表,可能来自导入 CSV。如果您想通过其中一个属性 (Name
) 的部分匹配列表 (Fiddler
、Chrome
) 过滤这些对象,最好的方法是构建一个正则表达式:
$excludeApps = 'Fiddler', 'Chrome'
$expr = @($excludeApps | ForEach-Object { [regex]::Escape($_) }) -join '|'
然后在 Where-Object
语句中使用 -notmatch
条件过滤列表:
$applications | Where-Object { $_.Name -notmatch $expr }
我有一个哈希表 ($applications
),看起来像这样:
Name Version ---- ------- Adobe Flash Player 19 ActiveX 19.0.0.226 Enterprise Library 4.0 - May 2008 4.0.0.0 Entity Framework Designer for Visual Studio 2012 - enu 11.1.21009.00 Fiddler 2.4.0.6 Google Chrome 49.0.2623.87 IIS 8.0 Express 8.0.1557
所以我试图从列表中排除一些应用程序,为此我正在使用:
[array]$excludeApps = "Fiddler","Chrome"
foreach ($excludeApp in $excludeApps){
$applications = $applications -notlike "*$excludeApp*"
}
结果可能过滤掉了排除列表,但与我预期的不同:
@{Name=Adobe Flash Player 19 ActiveX; Version=19.0.0.226} @{Name=Enterprise Library 4.0 - May 2008; Version=4.0.0.0} @{Name=Entity Framework Designer for Visual Studio 2012 - enu; Version=11.1.21009.00} @{Name=IIS 8.0 Express; Version=8.0.1557}
我尝试使用 GetEnumerator()
和 ${applications.Name}
的几种语法来处理这些值,但没有任何效果。我相信 PowerShell 将此列表检测为字符串。
知道如何处理吗?
将 select-对象与 -match/-notmatch
运算符一起使用。
即:
$application = $application | ? { $_.name -notmatch "fiddler" -or $_.name -notmatch "Chrome"}
或在循环中:
[array]$excludeApps = "Fiddler","Chrome"
foreach ($excludeApp in $excludeApps){
$applications = $applications | ? {$_.name -notmatch $excludeApp }
}
一方面,您的数据不是哈希表,而是自定义对象列表,可能来自导入 CSV。如果您想通过其中一个属性 (Name
) 的部分匹配列表 (Fiddler
、Chrome
) 过滤这些对象,最好的方法是构建一个正则表达式:
$excludeApps = 'Fiddler', 'Chrome'
$expr = @($excludeApps | ForEach-Object { [regex]::Escape($_) }) -join '|'
然后在 Where-Object
语句中使用 -notmatch
条件过滤列表:
$applications | Where-Object { $_.Name -notmatch $expr }