并使用 where 对象查询

and query with where object

我想弄清楚我遗漏了什么或如何重写下面的内容...我想做的是确保我有多个驱动器号,如 E、T、J、K。 .当我只是 运行 get-psdrive 所有那些 letters/NAME 都存在但我无法得到和查询工作...我不能使用 OR 查询,因为我需要确保以上所有 dirve( E、T、J、K) 存在...

PS C:\Users\test> get-psdrive | where { $_.Name -eq 'E'}

Name           Used (GB)     Free (GB) Provider      Root
----           ---------     --------- --------      ----
E                    .19         49.81 FileSystem    E:\


PS C:\Users\test> get-psdrive | where { $_.Name -eq 'E' -and $_.Name -eq 'T' }
PS C:\Users\test>

已使用 mjolinor -LIKE 方法更新,因为它在此示例中也能更好地工作。这将查找驱动器,然后与驱动器进行比较,列出的任何内容都被视为 PSDrives 列表中缺失。

$Drives = 'E','T','J','K'
Try {
    Compare-Object (Get-PSDrive | Where  {
        $_.Name -like "[$(-join $Drives)]"
    } | Select -Expand Name) $Drives | Where {
        $_.SideIndicator -eq '=>'
    }
} Catch {
    If ($_.exception.message -like '*because it is null*') {
        $Drives | ForEach {
            New-Object PSObject -Property @{
                InputObject = $_
                SideIndicator = '=>'
            }
        }
    } Else {
        Write-Warning $_
    }
}

您可以使用 -like:

来简化它
get-psdrive | where { $_.Name -like '[ET]' }