max scalf
max scalf

Reputation: 329

and query with where object

I am trying to figure out what am i missing or how to rewrite the below...what i am trying to do is make sure i have multiple drive letters like E, T, J, K are present...when i just run get-psdrive all those letters/NAME are present but i cant get the and query to work...i cant use the OR query as i need to make sure all the above dirve(E, T, J, K) are present...

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>

Upvotes: 0

Views: 110

Answers (2)

boeprox
boeprox

Reputation: 1868

Updated using mjolinor -LIKE approach because it works better in this example as well. This will look for the drives and then performs a compare against the drives and anything listed are considered missing from the list of 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 $_
    }
}

Upvotes: 1

mjolinor
mjolinor

Reputation: 68243

You can simplify that using -like:

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

Upvotes: 0

Related Questions