在PowerShell中,如何确定当前驱动器是否为联网驱动器?
-
03-07-2019 - |
题
我需要知道,在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}
对接受的答案略微更紧凑:
[System.IO.DriveInfo]("C")
更进一步,如下所示:
([System.IO.DriveInfo]("C")).Drivetype
请注意,这仅适用于本地系统。将WMI用于远程计算机。
最可靠的方法是使用WMI
get-wmiobject win32_volume | ? { 最可靠的方法是使用WMI
<*>
DriveType是以下值的枚举
0 - 未知
1 - 没有根目录
2 - 可移动磁盘
3 - 本地磁盘
4 - 网络驱动器
5 - 光盘
6 - RAM磁盘
.DriveType -eq 4 } | % { get-psdrive 最可靠的方法是使用WMI
<*>
DriveType是以下值的枚举
0 - 未知
1 - 没有根目录
2 - 可移动磁盘
3 - 本地磁盘
4 - 网络驱动器
5 - 光盘
6 - RAM磁盘
.DriveLetter[0] }
DriveType是以下值的枚举
0 - 未知 1 - 没有根目录 2 - 可移动磁盘 3 - 本地磁盘 4 - 网络驱动器 5 - 光盘 6 - RAM磁盘