2017-02-28 50 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 
}