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 { } else {
Copy-Item $newestKey.Path $TargetKey -Force Copy-Item $newestKey.Path $TargetKey -Force
$acl = Get-Acl $TargetKey try {
$acl.SetAccessRuleProtection($true, $false) $acl = Get-Acl $TargetKey -ErrorAction SilentlyContinue
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow") if ($acl) {
$acl.SetAccessRule($rule) $acl.SetAccessRuleProtection($true, $false)
$adminRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators", "FullControl", "Allow") $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:USERNAME", "FullControl", "Allow")
$acl.SetAccessRule($adminRule) $acl.SetAccessRule($rule)
Set-Acl -Path $TargetKey -AclObject $acl $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 ""
Write-Host " Rotated: $($newestKey.Path)" -ForegroundColor Green Write-Host " Rotated: $($newestKey.Path)" -ForegroundColor Green