2013-06-04 102 views
13

在PowerShell中,我需要解析结点(符号链接)的目标路径。powershell解决目标路径结点

例如,说我有一个结c:\someJunction其目标是c:\temp\target

我试过的$junc = Get-Item c:\someJunction变化,但只能得到c:\someJunction

如何找到交界的目标路径,以给定交叉点的这个例子c:\temp\target

回答

2

您可以通过执行得到以下路径:

Get-ChildItem -Path C:\someJunction 

编辑查找路径和文件夹

Add-Type -MemberDefinition @" 
private const int FILE_SHARE_READ = 1; 
private const int FILE_SHARE_WRITE = 2; 

private const int CREATION_DISPOSITION_OPEN_EXISTING = 3; 
private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; 

[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)] 
public static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags); 

[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)] 
public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, 
IntPtr SecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); 

public static string GetSymbolicLinkTarget(System.IO.DirectoryInfo symlink) 
{ 
    SafeFileHandle directoryHandle = CreateFile(symlink.FullName, 0, 2, System.IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, System.IntPtr.Zero); 
    if(directoryHandle.IsInvalid) 
    throw new Win32Exception(Marshal.GetLastWin32Error()); 

    StringBuilder path = new StringBuilder(512); 
    int size = GetFinalPathNameByHandle(directoryHandle.DangerousGetHandle(), path, path.Capacity, 0); 
    if (size<0) 
    throw new Win32Exception(Marshal.GetLastWin32Error()); 
    // The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\" 
    // More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx 
    if (path[0] == '\\' && path[1] == '\\' && path[2] == '?' && path[3] == '\\') 
    return path.ToString().Substring(4); 
    else 
    return path.ToString(); 
} 
"@ -Name Win32 -NameSpace System -UsingNamespace System.Text,Microsoft.Win32.SafeHandles,System.ComponentModel 

$dir = Get-Item D:\1 
[System.Win32]::GetSymbolicLinkTarget($dir) 
+0

[这看起来一样的东西。(http://techibee.com/powershell/read-target-folder-of-a-symlink-using-powershell/1916) –

+0

@BobLobLaw实际上得到了它从http://chrisbensen.blogspot.ru/2010/06/getfinalpathnamebyhandle.html – Josh

+0

谢谢,它的工作!我觉得我必须抱怨为了得到像路口目标目标这样简单的东西所需的努力,但这不是你的错......我的牛肉与力量。 – ash

2

该做的伎俩用更少的工作没有内容,甚至可以用于远程服务器上的路口:

fsutil reparsepoint query "M:\Junc" 

如果您只想目标名称:

fsutil reparsepoint query "M:\Junc" | where-object { $_ -imatch 'Print Name:' } | foreach-object { $_ -replace 'Print Name\:\s*','' } 

所以

function Get_JunctionTarget($p_path) 
{ 
    fsutil reparsepoint query $p_path | where-object { $_ -imatch 'Print Name:' } | foreach-object { $_ -replace 'Print Name\:\s*','' } 
} 

此外,下面的代码是乔希上面提供的代码稍加修改。它可以被放置在被多次读取一个文件,并在网络驱动器的情况下,正确处理主导\\?\

function Global:Get_UNCPath($l_dir) 
{ 
    if((([System.Management.Automation.PSTypeName]'System.Win32').Type -eq $null) -or ([system.win32].getmethod('GetSymbolicLinkTarget') -eq $null)) 
    { 
     Add-Type -MemberDefinition @" 
private const int CREATION_DISPOSITION_OPEN_EXISTING = 3; 
private const int FILE_FLAG_BACKUP_SEMANTICS = 0x02000000; 

[DllImport("kernel32.dll", EntryPoint = "GetFinalPathNameByHandleW", CharSet = CharSet.Unicode, SetLastError = true)] 
public static extern int GetFinalPathNameByHandle(IntPtr handle, [In, Out] StringBuilder path, int bufLen, int flags); 

[DllImport("kernel32.dll", EntryPoint = "CreateFileW", CharSet = CharSet.Unicode, SetLastError = true)] 
public static extern SafeFileHandle CreateFile(string lpFileName, int dwDesiredAccess, int dwShareMode, 
IntPtr SecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile); 

public static string GetSymbolicLinkTarget(System.IO.DirectoryInfo symlink) 
{ 
    SafeFileHandle directoryHandle = CreateFile(symlink.FullName, 0, 2, System.IntPtr.Zero, CREATION_DISPOSITION_OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, System.IntPtr.Zero); 
    if(directoryHandle.IsInvalid) 
    { 
     throw new Win32Exception(Marshal.GetLastWin32Error()); 
    } 
    StringBuilder path = new StringBuilder(512); 
    int size = GetFinalPathNameByHandle(directoryHandle.DangerousGetHandle(), path, path.Capacity, 0); 
    if (size<0) 
    { 
     throw new Win32Exception(Marshal.GetLastWin32Error()); 
    } 
    // The remarks section of GetFinalPathNameByHandle mentions the return being prefixed with "\\?\" 
    // More information about "\\?\" here -> http://msdn.microsoft.com/en-us/library/aa365247(v=VS.85).aspx 
    string sPath = path.ToString(); 
    if(sPath.Length>8 && sPath.Substring(0,8) == @"\\?\UNC\") 
    { 
     return @"\" + sPath.Substring(7); 
    } 
    else if(sPath.Length>4 && sPath.Substring(0,4) == @"\\?\") 
    { 
     return sPath.Substring(4); 
    } 
    else 
    { 
     return sPath; 
    } 
} 
"@ -Name Win32 -NameSpace System -UsingNamespace System.Text,Microsoft.Win32.SafeHandles,System.ComponentModel 
    } 
    [System.Win32]::GetSymbolicLinkTarget($l_dir) 
} 

并给予上述功能Get_UNCPath,我们可以改善功能Get_JunctionTarget如下:

function Global:Get_JunctionTarget([string]$p_path) 
{ 
    $l_target = fsutil reparsepoint query $p_path | where-object { $_ -imatch 'Print Name\:' } | foreach-object { $_ -replace 'Print Name\:\s*','' } 
    if($l_target -imatch "(^[A-Z])\:\\") 
    { 
     $l_drive = $matches[1] 
     $l_uncPath = Get_UncPath $p_path 
     if($l_uncPath -imatch "(^\\\\[^\\]*\\)") 
     { 
      $l_machine = $matches[1] 
      $l_target = $l_target -replace "^$l_drive\:","$l_machine$l_drive$" 
     } 
    } 
    $l_target 
} 
+0

谢谢......第一个命令对我非常有效。它花了一些时间才意识到输出包括路径的十六进制字节......以及ascii路径(在右边)。 – Xantix

2

我们最终使用此功能

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 '' 
} 
+1

只有像'C:\ Users \ Joe \ SendTo'这样的系统重新分析点的解决方案afaict – Frank

13

已增强New-Item,Remove-Item和Get-ChildItem以支持创建和管理符号链接。 New-Item的-ItemType参数接受一个新值SymbolicLink。现在,您可以通过运行New-Item cmdlet在一行中创建符号链接。

What's New in Windows PowerShell

我检查了我的Windows 7机器上的符号链接的支持,它的正常工作。

> New-Item -Type SymbolicLink -Target C: -Name TestSymlink 


    Directory: C:\Users\skokhanovskiy\Desktop 


Mode    LastWriteTime   Length Name 
----    -------------   ------ ---- 
d----l  06.09.2016  18:27    TestSymlink 

获得符号链接的目标和创建目标一样简单。

> Get-Item .\TestSymlink | Select-Object -ExpandProperty Target 
C:\ 
+0

呵呵。谢谢你。我将不得不进行调查。 –