2015-09-02 47 views
0

我想弄清楚我缺少什麼或如何重寫下面...我試圖做的是確保我有多個驅動器字母像E,T,J,K是目前...當我只是運行get-psdrive所有這些字母/名稱都存在,但我不能得到和查詢工作...我不能使用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> 
+0

'GET-PSDrive來E,T |外空; $' – PetSerAl

回答

1

使用mjolinor樣的方法,因爲它在這個例子中更好以及更新。這將查找驅動器,然後執行與驅動器的比較,並且列出的任何事物都被認爲從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 $_ 
    } 
} 
+0

但該查詢將返回TRUE即使 「T」 的盤符is missing .... –

+0

我根據您的要求更新了代碼,並使用mjolinor的-LIKE方法,因爲在我的示例中它更容易工作。 – boeprox

0

可以簡化使用樣?

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