Skip to content

Commit dca5d00

Browse files
author
Steve Lee (POWERSHELL)
committed
address Jason's feedback
1 parent 95db3e4 commit dca5d00

2 files changed

Lines changed: 10 additions & 6 deletions

File tree

src/System.Management.Automation/namespaces/FileSystemProvider.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,10 +2434,14 @@ protected override void NewItem(
24342434

24352435
private static bool WinCreateSymbolicLink(string path, string strTargetPath, bool isDirectory)
24362436
{
2437-
int created = NativeMethods.CreateSymbolicLink(
2438-
path,
2439-
strTargetPath,
2440-
(isDirectory ? NativeMethods.SymbolicLinkFlags.Directory : NativeMethods.SymbolicLinkFlags.File) | NativeMethods.SymbolicLinkFlags.AllowUnprivilegedCreate);
2437+
// The new AllowUnprivilegedCreate is only available on Win10 build 14972 or newer
2438+
var flags = isDirectory ? NativeMethods.SymbolicLinkFlags.Directory : NativeMethods.SymbolicLinkFlags.File;
2439+
if (Environment.OSVersion.Version.Major >= 10 && Environment.OSVersion.Version.Build >= 14972)
2440+
{
2441+
flags |= NativeMethods.SymbolicLinkFlags.AllowUnprivilegedCreate;
2442+
}
2443+
2444+
int created = NativeMethods.CreateSymbolicLink(path, strTargetPath, flags);
24412445
return (created == 1) ? true : false;
24422446
}
24432447

test/powershell/Modules/Microsoft.PowerShell.Management/New-Item.Tests.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,13 @@ Describe "New-Item with links fails for non elevated user if developer mode not
269269
Remove-Item -Path $testFilePath -Force -ErrorAction SilentlyContinue
270270
}
271271

272-
It "Should error correctly when failing to create a symbolic link and not in developer mode" -Skip:(!$IsWindows -or $developerMode) {
272+
It "Should error correctly when failing to create a symbolic link and not in developer mode" -Skip:(!$IsWindows -or $developerMode -or (Test-IsElevated)) {
273273
{ New-Item -ItemType SymbolicLink -Path $TestFilePath -Target $FullyQualifiedFile -ErrorAction Stop } |
274274
Should -Throw -ErrorId "NewItemSymbolicLinkElevationRequired,Microsoft.PowerShell.Commands.NewItemCommand"
275275
$TestFilePath | Should -Not -Exist
276276
}
277277

278-
It "Should succeed to create a symbolic link without elevation and in developer mode" -Skip:(!$IsWindows -or !$developerMode) {
278+
It "Should succeed to create a symbolic link without elevation and in developer mode" -Skip:(!$IsWindows -or !$developerMode -or (Test-IsElevated)) {
279279
{ New-Item -ItemType SymbolicLink -Path $TestFilePath -Target $FullyQualifiedFile -ErrorAction Stop } | Should -Not -Throw
280280
$TestFilePath | Should -Exist
281281
}

0 commit comments

Comments
 (0)