Skip to main content

Grant Registry Permissions

I needed to configure several of our severs to grant authenticated users the ability to create event log sources. I have done this in the past, but I hated the syntax, so, I created a small little function that makes it cleaner for my fellow admins to read. I really wish small things like this were built-in.
  1. function Grant-RegistryPermission {  
  2.     param(  
  3.         [Parameter(Mandatory=$true)]  
  4.         [string] $Path,  
  6.         [Parameter(Mandatory=$true)]  
  7.         [string] $Identity,  
  9.         [Parameter(Mandatory=$true)]  
  10.         [Security.AccessControl.RegistryRights] $Rights,  
  12.         [Security.AccessControl.InheritanceFlags] $Inheritance = [Security.AccessControl.InheritanceFlags]::None,  
  13.         [Security.AccessControl.PropagationFlags] $Propagation = [Security.AccessControl.PropagationFlags]::None  
  14.     )  
  16.     $rule = New-Object Security.AccessControl.RegistryAccessRule $Identity,$Rights,$Inheritance,$Propagation,Allow  
  18.     $acl = Get-Acl $Path  
  19.     $acl.AddAccessRule($rule)  
  21.     Set-Acl -Path $Path -Acl $acl  
  22. }  
  24. # Security event log is locked down on 2008+  
  25. # The apps fail to enumerate past it when creating new event sources  
  26. # Give read-only access  
  27. Grant-RegistryPermission -Path HKLM:\SYSTEM\CurrentControlSet\Services\eventlog -Identity 'NT AUTHORITY\Authenticated Users' -Rights 'SetValue,CreateSubKey,ReadKey'
  28. Grant-RegistryPermission -Path HKLM:\SYSTEM\CurrentControlSet\Services\eventlog\Security -Identity 'NT AUTHORITY\Authenticated Users' -Rights ReadKey -Inheritance ContainerInherit  
  29. Grant-RegistryPermission -Path HKLM:\SYSTEM\CurrentControlSet\Services\eventlog\Application -Identity 'NT AUTHORITY\Authenticated Users' -Rights 'SetValue,CreateSubKey,ReadKey' -Inheritance ContainerInherit  


Popular posts from this blog

PowerShell SupportsShouldProcess Worst & Best Practices

This has been a very big discussion within the Scripting Games 2013 community and I want to add my two cents in an official blog post.

I've left several people comments on how they might be misunderstanding how SupportsShouldProcess works, but I also realize, everyone of these individuals has given me more insight into its use and perhaps, how it should best be utilized.

For those of you that don't know, SupportsShouldProcess is a parameter on the CmdletBinding attribute you can place on your cmdlets that automatically adds the -WhatIf and -Confirm parameters. These will naturally flow into other cmdlets you use that also SupportsShouldProcess, e.g. New-Item, Move-Item.

The major discussion has been around, should you just let the other cmdlets handle the $PSCmdlet.ShouldProcess feature, and if not how should you implement it. ShouldProcess has the following definitions.

Generate Random SecureString Key

Ever need to encrypt a SecureString that can be used across multiple servers? I suggest storing this BASE64 value in a secure location only accessible by the account(s) that need to decrypt the SecureString.
$secret = 'secret1234'$key    = [Convert]::ToBase64String((1..32 |% { [byte](Get-Random -Minimum 0 -Maximum 255) }))  $encryptedSecret = ConvertTo-SecureString -AsPlainText -Force -String $secret | ConvertFrom-SecureString -Key ([Convert]::FromBase64String($key))  $encryptedSecret | Select-Object @{Name='Key';Expression={$key}},@{Name='EncryptedSecret';Expression={$encryptedSecret}} | fl  $ss = ConvertTo-SecureString -Key ([Convert]::FromBase64String($key)) -String $encryptedSecret(New-Object System.Management.Automation.PSCredential 'SECURESTRING',$ss).GetNetworkCredential().Password

PowerShell Error Handling Behavior Debunked

Note: I am using simple error messages as an example, please reference the best practices and guidelines I outlined on when to use custom error messages.

I have been churning in my mind for the last few days all the entries in the 2013 Scripting Games and how they handle errors, or lack thereof.

I am coming to the conclusion through some testing that the simple fact of seeing a try..catch or throw statements does not mean there is proper error handling.

I've been testing several variations and forms of error handling, so lets start with the basics.
function Test-WriteError {      [CmdletBinding()] param()  "Test-WriteError::ErrorActionPreference = $ErrorActionPreference"Move-Item -Path 'C:\Does\Not\Exists.log' -Destination 'C:\No\Where'"Test-WriteError::End"}   Test-WriteError::ErrorActionPreference = Continue
Move-Item : Cannot find path 'C:\Does\Not\Exists.log' because it does not exist.
At line:6 char:5
+     Move-Item -Path 'C:\Does\N…