首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何创建相关的符号链接powershell方式?

如何创建相关的符号链接powershell方式?
EN

Stack Overflow用户
提问于 2017-02-28 16:18:00
回答 4查看 3K关注 0票数 9

在powershell 5.1中创建相对符号链接并不那么简单。New-Item没有像预期的那样工作。下文列出了一些办法。我是不是遗漏了什么?

所有示例的示例设置:

代码语言:javascript
复制
mkdir C:\Temp\foo -ErrorAction SilentlyContinue
'sample contents' > C:\Temp\foo\foo.txt
cd C:\Temp

Sample1:不像预期的那样工作吗

代码语言:javascript
复制
#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

Sample2:不像预期的那样工作吗

代码语言:javascript
复制
#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

Sample3:按预期工作

代码语言:javascript
复制
#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:按预期工作

代码语言:javascript
复制
#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

编辑: Sample3已更新为unicode条目和GetLastError。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2021-02-15 13:57:48

从pwsh7.1.X开始,sample1按预期工作:

代码语言:javascript
复制
#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
票数 1
EN

Stack Overflow用户

发布于 2018-02-16 01:53:21

您可以尝试将此函数添加到$PROFILE中。

代码语言:javascript
复制
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
}
票数 0
EN

Stack Overflow用户

发布于 2020-06-15 22:28:31

试着做这样的事情:

代码语言:javascript
复制
$fld = [string]"C:\Temp\bar"
$fl = [string]"C:\Temp\foo\foo.txt"

# Link to File
New-Item -ItemType SymbolicLink -Target $fl -Path $fld  -Force

# Link to Directory
New-Item -ItemType Junction -Path C:\Temp\bar -Target C:\Temp\foo -Force
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42513791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档