PowerShell:比较2个变量

时间:2012-05-18 16:09:51

标签: variables powershell comparison

任何人都可以告诉我为什么下面的代码不起作用?我想最终得到匹配两者的驱动器,例如F:和G:

我知道它很简单,但无法弄明白。欢迎快速解释。谢谢

$USBDrives =$null 
$WMIUSBDrives="E:","F:","G:"
$SystemDrives="D:","F:","G:"
$USBDrives = $SystemDrives | Where {$_ -contains $WMIUSBDrives}
$USBDrives

2 个答案:

答案 0 :(得分:3)

你实际上应该用这种方式......:)

$USBDrives = $SystemDrives | Where {$WMIUSBDrives -contains $_}
$USBDrives

以您想要的方式运行的运算符是在v3中添加的-in。 HTH 鲍尔泰克

答案 1 :(得分:0)

为了实现上述目标,我认为$ WMIUSBDrives和$ SystemDrives必须匹配。

请改为尝试:

$USBDrives =$null 
$WMIUSBDrives="E:","F:","G:"
$SystemDrives="D:","F:","G:"
foreach($drive in $WMIUSBDrives)
{
    if($SystemDrives  -contains $drive){$USBDrives += $drive}
}
$USBDrives