fix: wrap Set-Acl in try/catch to avoid SeSecurityPrivilege error on SSH key rotate
This commit is contained in:
1 parent
5d19f772e5
commit
32517999f3
1 file changed
+11
-7
+11
-7
@@ -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
|
||||||
|
|||||||
Reference in new issue
Block a user