0

谁能告诉我为什么下面的代码不起作用?我想最终得到与 F: 和 G: 匹配的驱动器

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

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

2 回答 2

3

您实际上应该以其他方式使用... :)

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

以您想要的方式工作的运算符是 -in,它是在 v3 中添加的。HTH 巴特克

于 2012-05-18T18:18:38.673 回答
0

为了使上述方法起作用,我认为 $WMIUSBDrives 和 $SystemDrives 必须匹配。

试试这个:

$USBDrives =$null 
$WMIUSBDrives="E:","F:","G:"
$SystemDrives="D:","F:","G:"
foreach($drive in $WMIUSBDrives)
{
    if($SystemDrives  -contains $drive){$USBDrives += $drive}
}
$USBDrives
于 2012-05-18T16:22:09.157 回答