Created
November 16, 2020 22:42
-
-
Save thereisnotime/7c9df1ab50d4d5feaa20082dad71ecc0 to your computer and use it in GitHub Desktop.
Windows - Fix private SSH key file permissions, ownership and inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Set-PrivateKeyPermissions { | |
param ( | |
[string]$KeyLocation | |
) | |
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { | |
Write-Error "Must run as administrator. Exiting..." | |
Return | |
} | |
$acl = Get-Acl "$KeyLocation" | |
$currentUser = New-Object System.Security.Principal.Ntaccount($acl.owner) | |
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule($acl.owner,"FullControl","Allow") | |
$acl.Access | %{$acl.RemoveAccessRule($_)} | |
$acl.SetOwner($currentUser) | |
$acl.SetAccessRuleProtection($true,$false) | |
$acl.SetAccessRule($accessRule) | |
$acl | Set-Acl "$KeyLocation" | |
} | |
# Example usage: | |
Set-PrivateKeyPermissions -KeyLocation 'C:\Keys\thereisnotime.pem' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment