【问题标题】:In PowerShell, how can I determine the root of a drive (supposing it's a networked drive)在 PowerShell 中,如何确定驱动器的根目录(假设它是网络驱动器)
【发布时间】:2010-09-14 14:01:00
【问题描述】:

在 PowerShell 中,即使可以知道驱动器是否为网络驱动器:请参阅 In PowerShell, how can I determine if the current drive is a networked drive or not?

当我尝试获取驱动器的“根”时,我得到了驱动器号。

设置: 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.

Get-PSDrive 告诉我们 Root 是 H:

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

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

并且使用 system.io.driveinfo 并没有给我们一个完整的答案:

PS:13 H:\
>$x = new-object system.io.driveinfo("h:\")
PS:14 H:\
>$x.DriveType
Network
PS:15 H:\
>$x.RootDirectory

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----        29/09/2008     16:45            h:\

知道如何获取这些信息吗?

谢谢

【问题讨论】:

    标签: powershell networking mapped-drive


    【解决方案1】:

    诀窍在于属性名称与预期不同。 试试:

    (Get-PSDrive h).DisplayRoot

    【讨论】:

      【解决方案2】:

      试试 WMI:

      Get-WMIObject -query "Select ProviderName From Win32_LogicalDisk Where DeviceID='H:'"
      

      【讨论】:

      • 一个小问题,它是 Get-WMIObject 而不是 Get-WMI :)
      • 是的。我会编辑。我实际上使用了 gwmi 别名,然后打算对其进行去别名以进行发布,并且只是部分成功。
      【解决方案3】:

      $drive = gwmi win32_logicaldisk -filter "DeviceID='H:'" if($drive.DriveType -eq 4) {write-host "驱动器是网络共享"}

      【讨论】:

        【解决方案4】:

        $fso=new-object -com "Scripting.Filesystemobject" $fso.GetDrive("Y").ShareName

        【讨论】:

          猜你喜欢
          • 2010-09-14
          • 1970-01-01
          • 2012-06-03
          • 2011-05-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-22
          • 2014-08-29
          相关资源
          最近更新 更多