gpt4 book ai didi

powershell - 如何创建相对符号链接(symbolic link) powershell 方式?

转载 作者:行者123 更新时间:2023-12-02 11:22:46 24 4
gpt4 key购买 nike

创建 没那么简单相对 powershell 5.1 中的符号链接(symbolic link)。 New-Item没有按预期工作。下面列出了一些方法。我错过了什么吗?

所有示例的示例设置:

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

示例 3:按预期工作
#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")

示例 4:按预期工作
#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 api 条目和 GetLastError

最佳答案

从 pwsh 7.1.X sample1 开始按预期工作:

#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

关于powershell - 如何创建相对符号链接(symbolic link) powershell 方式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42513791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com