我需要知道,在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用于远程计算机。

scroll top