In PowerShell, how can I determine the root of a d

2019-08-02 20:58发布

In PowerShell, even if it's possible to know if a drive is a network drive: see In PowerShell, how can I determine if the current drive is a networked drive or not?

When I try to get the "root" of the drive, I get back the drive letter.

The setup: 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.

Get-PSDrive tells us that the Root is H:

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

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

and using system.io.driveinfo does not give us a complete answer:

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:\

Any idea of how to get that info?

Thanks

4条回答
聊天终结者
2楼-- · 2019-08-02 21:13

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

查看更多
来,给爷笑一个
3楼-- · 2019-08-02 21:19

$drive = gwmi win32_logicaldisk -filter "DeviceID='H:'" if($drive.DriveType -eq 4) {write-host "drive is a network share"}

查看更多
贪生不怕死
4楼-- · 2019-08-02 21:27

The trick is that the attribute name is different than expected. Try:

(Get-PSDrive h).DisplayRoot

查看更多
Anthone
5楼-- · 2019-08-02 21:35

Try WMI:

Get-WMIObject -query "Select ProviderName From Win32_LogicalDisk Where DeviceID='H:'"
查看更多
登录 后发表回答