fix: wrap Set-Acl in try/catch to avoid SeSecurityPrivilege error on SSH key rotate

This commit is contained in:
Reditus committed 2026-06-01 05:47:04 +01:00
1 parent 5d19f772e5
commit 32517999f3
1 file changed
+11 -7
+11 -7
View File
@@ -2821,13 +2821,17 @@ function Rotate-SSHKey {
} else {
Copy-Item $newestKey.Path $TargetKey -Force
$acl = Get-Acl $TargetKey
$acl.SetAccessRuleProtection($true, $false)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow")
$acl.SetAccessRule($rule)
$adminRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators", "FullControl", "Allow")
$acl.SetAccessRule($adminRule)
Set-Acl -Path $TargetKey -AclObject $acl
try {
$acl = Get-Acl $TargetKey -ErrorAction SilentlyContinue
if ($acl) {
$acl.SetAccessRuleProtection($true, $false)
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow")
$acl.SetAccessRule($rule)
$adminRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators", "FullControl", "Allow")
$acl.SetAccessRule($adminRule)
Set-Acl -Path $TargetKey -AclObject $acl -ErrorAction SilentlyContinue
}
} catch {}
Write-Host ""
Write-Host " Rotated: $($newestKey.Path)" -ForegroundColor Green