Skip to content

Instantly share code, notes, and snippets.

@grenade
Created October 8, 2014 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grenade/c3b591983f1e90d50ea3 to your computer and use it in GitHub Desktop.
Save grenade/c3b591983f1e90d50ea3 to your computer and use it in GitHub Desktop.
Allow log4net to create eventlog appenders under the user account specified by $username
param (
[string] $username = "$env:USERDOMAIN\$env:USERNAME"
)
function GrantAccess ($principle, $log, $access) {
$path = ('HKLM:\SYSTEM\CurrentControlSet\services\eventlog\{0}' -f $log)
if (Test-Path $path) {
Write-Host ('Granting {0} on {1} to {2}' -f $access, $path, $principle) -ForegroundColor Yellow
Get-Acl $path | Format-List
$acl = Get-Acl $path
$rule = New-Object System.Security.AccessControl.RegistryAccessRule($principle, @($access), 'Allow')
$acl.AddAccessRule($rule)
Set-Acl $path $acl
Write-Host ('Granted {0} on {1} to {2}' -f $access, $path, $principle) -ForegroundColor DarkGreen
Get-Acl $path | Format-List
} else {
Write-Error "Cannot acesss $path"
}
}
GrantAccess $username 'Application' 'ReadKey, WriteKey'
GrantAccess $username 'Security' 'ReadKey'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment