简体   繁体   English

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

[英]In PowerShell, how can I determine if the current drive is a networked drive or not?

I need to know, from within Powershell, if the current drive is a mapped drive or not. 我需要知道,在Powershell中,当前驱动器是否是映射驱动器。

Unfortunately, Get-PSDrive is not working "as expected": 不幸的是,Get-PSDrive没有像预期的那样工作:

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: 但在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.

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

Use the .NET framework: 使用.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:'"

An alternative way to use WMI: 另一种使用WMI的方法:

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

Get all network drives with: 获取所有网络驱动器:

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

The most reliable way is to use WMI 最可靠的方法是使用WMI

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

The DriveType is an enum wit hthe following values DriveType是以下值的枚举

0 - Unknown 1 - No Root Directory 2 - Removable Disk 3 - Local Disk 4 - Network Drive 5 - Compact Disk 6 - RAM Disk 0 - 未知1 - 无根目录2 - 可移动磁盘3 - 本地磁盘4 - 网络驱动器5 - 光盘6 - RAM磁盘

Here's a link to a blog post I did on the subject 这是我在这个主题上做的博客文章的链接

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

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

Take this a step further as shown below: 更进一步,如下所示:

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

Note this only works for the the local system. 请注意,这仅适用于本地系统。 Use WMI for remote computers. 将WMI用于远程计算机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM