1

In my environment, I have lots of Mount Point Drives (MPD hereafter), to simplify my question, let's say, I have a file whose full name is

d:\data\DBA\Perf\abc.sql

In this case, d:\data is actually an MPD but we do no know just from its face value. (i.e. it can be D:\ drive with a folder called \data\DBA)

How can I find out D:\Data is an MPD out of this file full name with PowerShell?

My environment is Windows 2012 R2 + PowerShell V5.

My initial solution is to find all MP drives (I have 10+ such MPDs, like d:\Log, e:\backup, d:\MP\Backup etc), and then try to loop through all MPDs to find whether an MPD name is contained in the file's full name. If so, I know abc.sql is in an MPD.

I am just wondering whether there is a better solution, something like this How to find the mountpoint a file resides on?. This solution is for Linux, but the key point of this solution is to test each directory contained in the path with os.path.ismount(path).

Can we have a similar solution in windows? Or you gurus may have better solution in Windows?

TIA,

Jeff_yao

jyao
  • 1,550
  • 3
  • 19
  • 26

1 Answers1

2

Use LinkType and Target properties of a file/directory object and work your way to the root:

function Get-ReparsePoints($path) {
    try { $file = Get-Item -literal $path } catch {}
    while ($file) {
        if ($file.LinkType) {
            [PSCustomObject]@{
                path   = $file.FullName
                target = $file.Target
                type   = $file.LinkType
            }
        }
        $file = if ($file.Parent) { $file.Parent } else { $file.Directory }
    }
}

Tested in PowerShell 5:

Get-ReparsePoints 'D:\lost\kill\!shared\mv\Lindsey Stirling\Radioactive.mp4'
path                       target                                          type    
----                       ------                                          ----    
D:\lost\kill\!shared\mv    {D:\lost\kill\mv}                               Junction
D:\lost                    {Volume{b2cd25bc-98e0-4cb1-bf51-f090f8dfda43}\} Junction
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • this is really great and helps a lot. You are a real guru. – jyao Jun 30 '17 at 20:57
  • @ wOxxOm, if you do not mind, it seems LinkType / Target properties are only available in PS V5, am I right? because I test your script in PowerShell V4, it seems not working while everything works fine in PS V5. :-) – jyao Jun 30 '17 at 21:00
  • @jyao, you're right I guess, the feature was added in PS5 apparently. – wOxxOm Jul 01 '17 at 10:07