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

时间:2020-03-06 14:58:59  来源:igfitidea点击:

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

不幸的是,Get-PSDrive无法"按预期"工作:

PS:24 H:\temp
>get-psdrive  h

Name       Provider      Root      CurrentLocation
----       --------      ----      ---------------
H          FileSystem    H:\          temp

但是在MS-Dos中,"网络使用"显示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}

最可靠的方法是使用WMI

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

DriveType是具有以下值的枚举

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

这是我在该主题上撰写的博客文章的链接

可接受的答案略微紧凑:

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

进一步执行此操作,如下所示:

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

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