powershell - -eq 是否适用于 Powershell 中的多个扩展

标签 powershell

嘿伙计们,

所以我在 powershell 中编写了一个小脚本(对此非常新)。大约 10 年没有碰过任何东西,但事情是这样的:

该脚本应该从 c:\上的特定文件夹中为我在网络上的所有计算机(包括我在不同州的 sibling 的一些计算机)提取证书日期,并将结果作为文本文件保存在我的桌面上。

我只是对这个扩展感到好奇。如果文件夹中有多个具有不同扩展名的证书,我可以使用 -eq 使用同一行提取两个扩展名的数据。假设文件夹路径是 C:\List\certificates (不会从证书存储中提取任何数据,因此没有 pfx 文件),我在代码中使用了这一行:

$certificates = Get-ChildItem -Path $using:certPath -Recurse | Where-Object { $_.Extension -eq “.cer .key”}

最佳答案

您可以使用 containment operator为此,-in-contains:

$certificates = Get-ChildItem -Path $using:certPath -Recurse |
    Where-Object Extension -In '.cer', '.key'

使用 Get-ChildItem 内置过滤参数 -Include 也应该有效:

$certificates = Get-ChildItem -Path $using:certPath -Recurse -Include *.cer, *.key

或者您可以使用 -Match 和正则表达式模式:

$certificates = Get-ChildItem -Path $using:certPath -Recurse |
    Where-Object Extension -Match '\.(?:cer|key)$'

关于powershell - -eq 是否适用于 Powershell 中的多个扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77525866/

相关文章:

performance - 读取CSV文件时Powershell拦截并修复特定值

powershell - 使用 Get-ChildItem 检索文件夹、名称、全名

powershell - 循环提前停止

windows - 如何在长 PowerShell 命令中使用变量而不破坏命令

powershell - 安装 NuGet 包时如何向项目添加 AfterBuild 事件?

powershell - Windows Powershell 读取制表符分隔的文件问题

powershell - 测试PowerShell中是否存在文件

powershell - 在屏幕和变量中调用表达式输出

powershell - 通过 PowerShell 脚本跟踪 AD 中的更改

powershell - 如何在PowerShell中使用CSOM连接到内部部署SharePoint 2013