powershell: get-psdrive and where-object



我试图在Windows 2008服务器上查找每个不是"C,E,L,S,T,W"的驱动器号。 谁能告诉我我的逻辑错误或我该怎么做?

[char[]]”CELSTW” | Where-Object {!(Get-PSDrive $_ )} 

您从不想要的驱动器号列表 (CELSTW( 开始,并输出不存在的驱动器号作为 psdrive。

你想要的是从一个所有PSDrives的列表开始,并在它们与你不想要的PSDrives匹配的地方过滤掉它们:

Get-PSDrive | Where-Object { [char[]]"CELSTW" -notcontains $_.Name }

尽管这将为您提供一堆其他PSDrive类型。 您可能还希望针对文件系统提供程序对其进行筛选:

 Get-PSDrive | Where-Object { [char[]]"CELSTW" -notcontains $_.Name -AND $_.Provider.Name -eq "FileSystem"}

这应该为您提供名称(驱动器号(不是"C,E,L,S,T,W"的所有ps驱动器

Get-PSDrive  | ?{[char[]]"CELSTW" -notcontains $_.name}

但是,如果要排除非文件系统 PS驱动器,请尝试以下操作:

Get-PSDrive  | ?{[char[]]"CELSTW" -notcontains $_.name} | ?{$_.Provider.name -eq "FileSystem"}

你必须从另一端去做:

$drives = [char[]]"CD"
Get-PSDrive | ? { $drives -notcontains $_.Name}

另一个使用 -notmatch 运算符的例子:

Get-PSDrive | Where-Object { $_.Name -notmatch '[CELSTW]'}

最新更新