在PowerShell中,如何确定当前驱动器是否为联网驱动器?

我需要知道,在Powershell中,当前驱动器是否是映射驱动器。

不幸的是,Get-PSDrive没有像预期的那样工作:

PS:24 H:\temp >get-psdrive h Name Provider Root CurrentLocation ---- -------- ---- --------------- H FileSystem H:\ temp 

但在MS-Dos中,“net use”表明H:实际上是一个映射的网络驱动器:

 New connections will be remembered. Status Local Remote Network ------------------------------------------------------------------------------- OK H: \\spma1fp1\JARAVJ$ Microsoft Windows Network The command completed successfully. 

我想要做的是获取驱动器的根目录并在提示符中显示它(请参阅: 自定义PowerShell提示符 – 相当于CMD的$ M $ P $ _ $ + $ G? )

使用.NET框架:

 PS H:\> $x = new-object system.io.driveinfo("h:\") PS H:\> $x.drivetype Network 

试试WMI:

 Get-WMI -query "Select ProviderName From Win32_LogicalDisk Where DeviceID='H:'" 

另一种使用WMI的方法:

get-wmiobject Win32_LogicalDisk | ? {$_.deviceid -eq "s:"} | % {$_.providername}

获取所有网络驱动器:

get-wmiobject Win32_LogicalDisk | ? {$_.drivetype -eq 4} | % {$_.providername}

接受答案的变化稍微紧凑:

 [System.IO.DriveInfo]("C") 

更进一步,如下所示:

 ([System.IO.DriveInfo]("C")).Drivetype 

请注意,这仅适用于本地系统。 将WMI用于远程计算机。

最可靠的方法是使用WMI

 get-wmiobject win32_volume | ? { $_.DriveType -eq 4 } | % { get-psdrive $_.DriveLetter[0] } 

DriveType是以下值的枚举

0 – 未知1 – 无根目录2 – 可移动磁盘3 – 本地磁盘4 – 网络驱动器5 – 光盘6 – RAM磁盘

这是我在这个主题上做的博客文章的链接