和查询对象的位置

时间:2015-09-02 14:00:10

标签: powershell powershell-v2.0

我想知道我错过了什么或者如何重写下面...我想要做的是确保我有多个驱动器字母,如E,T,J,K存在...当我只是运行get-psdrive所有那些字母/ 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>

2 个答案:

答案 0 :(得分:1)

使用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 $_
    }
}

答案 1 :(得分:0)

您可以使用-like简化:

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