在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?)
I need to know, from within Powershell, if the current drive is a mapped drive or not.
Unfortunately, Get-PSDrive is not working "as expected":
PS:24 H:\temp
>get-psdrive h
Name Provider Root CurrentLocation
---- -------- ---- ---------------
H FileSystem H:\ temp
but in MS-Dos "net use" shows that H: is really a mapped network drive:
New connections will be remembered.
Status Local Remote Network
-------------------------------------------------------------------------------
OK H: \\spma1fp1\JARAVJ$ Microsoft Windows Network
The command completed successfully.
What I want to do is to get the root of the drive and show it in the prompt (see: Customizing PowerShell Prompt - Equivalent to CMD's $M$P$_$+$G?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用.NET框架:
Use the .NET framework:
已接受答案的稍微更紧凑的变化:
A slightly more compact variation on the accepted answer:
尝试WMI:
Try WMI:
使用 WMI 的另一种方法:
get-wmiobject Win32_LogicalDisk | ? {$_.deviceid -eq“s:”}| % {$_.providername}
使用以下命令获取所有网络驱动器:
get-wmiobject Win32_LogicalDisk | ? {$_.drivetype -eq 4} | % {$_.providername}
An alternative way to use WMI:
get-wmiobject Win32_LogicalDisk | ? {$_.deviceid -eq "s:"} | % {$_.providername}
Get all network drives with:
get-wmiobject Win32_LogicalDisk | ? {$_.drivetype -eq 4} | % {$_.providername}
更进一步,如下所示:
请注意,这仅适用于本地系统。 对远程计算机使用 WMI。
Take this a step further as shown below:
Note this only works for the the local system. Use WMI for remote computers.
最可靠的方法是使用 WMI
DriveType 是一个枚举,具有以下值
0 - 未知
1 - 无根目录
2 - 可移动磁盘
3 - 本地磁盘
4 - 网络驱动器
5 - 光盘
6 - RAM 磁盘
这是我写的博客文章的链接 关于该主题
The most reliable way is to use WMI
The DriveType is an enum wit hthe following values
0 - Unknown
1 - No Root Directory
2 - Removable Disk
3 - Local Disk
4 - Network Drive
5 - Compact Disk
6 - RAM Disk
Here's a link to a blog post I did on the subject