PowerShell / PowerShell

PowerShell for every system!
https://microsoft.com/PowerShell
MIT License
44.27k stars 7.16k forks source link

Set-ACL on folder hangs, then times out PS 7.2.2 and 7.2.3 #17264

Open JazBInKC opened 2 years ago

JazBInKC commented 2 years ago

Prerequisites

Steps to reproduce

Moving a few million files and folders from SharePoint to NTFS. The code works perfectly under PS 5.1, but other (PnP) commands only worked or worked better under PowerShell 7. While running the script I noticed pauses in the script execution. I thought it was O365 throttling, turns out whenever I created a new folder and then set ownership via Set-ACL (note comments before and after Set-ACL), the process would hang for approx. 3 minutes then move on without error. Generally, the folder ownership was never updated. Again this code works flawlessly under PS 5.1. 20 new folders is one hours of wait time.

        if (!(Test-Path -literalpath $destinationfolder)) {
            Write-Host "Adding Folder: " $destinationfolder -ForegroundColor Green -BackgroundColor Black
            $dest = New-Item -Path $destinationfolder -type directory -ErrorAction SilentlyContinue -Force
            $acl = Get-Acl -LiteralPath $destinationfolder
            $acl.SetOwner([System.Security.Principal.NTAccount] "Domain Admins")
            #"Before Set-ACL"
            Set-Acl -LiteralPath $destinationfolder -AclObject $acl
            #"After Set-ACL"
            $global:folders += 1 
            Start-Sleep -m 250
        }
        else {
            Write-Host "Skipping Folder $destinationfolder" -ForegroundColor White -BackgroundColor Black
        }

Expected behavior

The ACL is swiftly updated or an error is returned

Actual behavior

Set-ACL hangs for 3 minutes then times out without error.

Error details

No error

Environment data

PS 7.2.3, .Net 6.0.4 runtime

Server 2016, fully patched.

Visuals

image

there will be no error - it will time out and the script will continue.

JazBInKC commented 2 years ago

Follow-up:

This seems to be tied to \\?\ long path support in PowerShell 7. In the example (pulled from my code) I attempt to Set-ACL. The first time with the long path prefix - takes 3 minutes. The second, without the prefix, happens instantly. As most users will be running "legacy" scripts - many supporting long path / max_path PowerShell should consistently support the prefix.

image

I should mention that the delay only seems to affect folder ACL, not files - go figure.

Regards,

jbw