Private/FixVagrantPrivateKeyPerms.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function FixVagrantPrivateKeyPerms {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory=$True)]
        [string]$PathToPrivateKey
    )

    ##### BEGIN Variable/Parameter Transforms and PreRun Prep #####

    $CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name

    if (! $(Test-Path $PathToPrivateKey)) {
        Write-Error "The path $PathToPrivateKey was not found! Halting!"
        $global:FunctionResult = "1"
        return
    }

    ##### END Variable/Parameter Transforms and PreRun Prep #####


    ##### BEGIN Main Body #####

    $SecurityDescriptor = Get-NTFSSecurityDescriptor -Path $PathToPrivateKey
    $SecurityDescriptor | Disable-NTFSAccessInheritance -RemoveInheritedAccessRules
    $SecurityDescriptor | Clear-NTFSAccess
    $SecurityDescriptor | Add-NTFSAccess -Account SYSTEM -AccessRights FullControl -AppliesTo ThisFolderOnly
    $SecurityDescriptor | Add-NTFSAccess -Account $CurrentUser -AccessRights FullControl -AppliesTo ThisFolderOnly
    $SecurityDescriptor | Set-NTFSSecurityDescriptor

    ##### END Main Body #####
}