2017-02-28 53 views
5

这不是那么简单的创建相对 PowerShell 5.1中的符号链接。 New-Item未按预期工作。下面列出了一些方法。我错过了什么吗?如何创建相对符号链接PowerShell的方式?

对于上述所有样品的设置:

mkdir C:\Temp\foo -ErrorAction SilentlyContinue 
'sample contents' > C:\Temp\foo\foo.txt 
cd C:\Temp 

样本1:是否按预期方式工作

#new ps5 Item cmdlets (https://msdn.microsoft.com/en-us/powershell/wmf/5.0/feedback_symbolic) are not working well with relative paths 
#C:\Temp\foo and C:\Temp\foo\foo.txt are returned 
$fld = New-Item -ItemType SymbolicLink -Name 'bar' -Target '.\foo' 
$fl = New-Item -ItemType SymbolicLink -Name 'bar.txt' -Target '.\foo\foo.txt' 
$fld.Target 
$fl.Target 

样品2:是否按预期方式工作

#Powershell community extensions 
#same problem - paths are created as absolute: C:\Temp\foo C:\Temp\foo\foo.txt 
$fld = New-Symlink 'c:\Temp\bar' '.\foo' 
$fl = New-Symlink 'c:\Temp\bar.txt' '.\foo\foo.txt' 
$fld.Target 
$fl.Target 

桑普LE3:按预期工作

#API call CreateSymbolicLink as per https://gallery.technet.microsoft.com/scriptcenter/new-symlink-60d2531e 
#.\foo and .\foo\foo.txt are returned 
Add-Type -MemberDefinition @' 
    [DllImport("kernel32.dll", EntryPoint = "CreateSymbolicLinkW", CharSet = CharSet.Unicode, SetLastError = true)] 
    public static extern bool CreateSymbolicLink(string lpSymlinkFileName, string lpTargetFileName, int dwFlags); 

    public static DirectoryInfo CreateSymbolicLinkToFolder(string lpSymlinkFileName, string lpTargetFileName) { 
     bool res = CreateSymbolicLink(lpSymlinkFileName, lpTargetFileName, 1); 
     if (!res) { throw new Win32Exception(Marshal.GetLastWin32Error()); } 
     return (new DirectoryInfo(lpSymlinkFileName)); 
    } 

    public static FileInfo CreateSymbolicLinkToFile(string lpSymlinkFileName, string lpTargetFileName) { 
     bool res = CreateSymbolicLink(lpSymlinkFileName, lpTargetFileName, 0); 
     if (!res) { throw new Win32Exception(Marshal.GetLastWin32Error()); } 
     return (new FileInfo(lpSymlinkFileName)); 
    } 
'@ -Name Win32 -NameSpace System -UsingNamespace System.ComponentModel, System.IO 
[Win32]::CreateSymbolicLinkToFolder("c:\Temp\bar", ".\foo") 
[Win32]::CreateSymbolicLinkToFile("c:\Temp\bar.txt", ".\foo\foo.txt") 

Sample4:按预期工作

#using mklink from cmd produces correct relative paths 
#.\foo and .\foo\foo.txt are returned 
cmd /c mklink /d "c:\Temp\bar" ".\foo" 
cmd /c mklink "c:\Temp\bar.txt" ".\foo\foo.txt" 
(Get-Item "c:\Temp\bar").Target 
(Get-Item "c:\Temp\bar.txt").Target 

编辑:样品3已经更新到Unicode API入口,并且GetLastError

+4

的PowerShell 6通过通配符解析[停止解析](https://github.com/PowerShell/PowerShell/commit/3a43126a2ea163f38c8d02c8177c79e954576ec6)新建项目的目标的完整路径的符号链接。所以在那之前你必须使用解决方法。 – wOxxOm

+0

@wOxxOm:谢谢你提供的信息 –

+2

@wOxxOm:不幸的是,你链接的提交只能添加目标不存在的项目,而不会改变将指定路径解析为绝对路径的行为(甚至不存在的目标被转换为绝对路径)。我创建了[此GitHub问题](https://github.com/PowerShell/PowerShell/issues/3500)来请求相关目标。 – mklement0

回答

0

,你可以尝试添加此功能,您的$ PROFILE 。

Function SymbolicLink-Add 
{ 
[CmdLetBinding()] 
Param ([string] $File, [string] $SymbolicLinkFolder) 

$LinkFile = (Get-Item $File) 

if ($false -eq $LinkFile.Exists) 
{ 
    return "Cannot create symbolic link file does not exist: [$File]" 
} 

if ($false -eq (Test-Path $SymbolicLinkFolder)) 
{ 
    New-Item -Type Directory -Path $SymbolicLinkFolder 
    -ErrorAction Stop 
} 

New-Item -ItemType SymbolicLink -Path $SymbolicLinkFolder -Name 
$LinkFile.Name -Value $LinkFile 
}