powershell to resolve junction target path

后端 未结 7 1106
闹比i
闹比i 2020-12-09 15:32

In PowerShell, I need resolve the target path of a junction (symlink).

for example, say I have a junction c:\\someJunction whose target is c:\\te

7条回答
  •  [愿得一人]
    2020-12-09 16:08

    We end up using this function

    function Get-SymlinkTargetDirectory {           
        [cmdletbinding()]
        param(
            [string]$SymlinkDir
        )
        $basePath = Split-Path $SymlinkDir
        $folder = Split-Path -leaf $SymlinkDir
        $dir = cmd /c dir /a:l $basePath | Select-String $folder
        $dir = $dir -join ' '
        $regx = $folder + '\ *\[(.*?)\]'
        $Matches = $null
        $found = $dir -match $regx
        if ($found) {
            if ($Matches[1]) {
                Return $Matches[1]
            }
        }
        Return '' 
    }
    

提交回复
热议问题