Setting AD logging

Now that we know what the options are we can look at switching them on.

function set-logsetting{             
[CmdletBinding(SupportsShouldProcess=$true)]             
param (             
[parameter(Position=0,            
   Mandatory=$true,            
   ValueFromPipeline=$true,             
   ValueFromPipelineByPropertyName=$true)]            
   [string]$computer,            
               
   [string]$setting,            
               
   [ValidateRange(0,5)]            
   [int]$level             
)             
BEGIN{            
 $HKLM = 2147483650            
}#begin             
            
PROCESS{            
 if ($logtype.Values -notcontains $setting){            
   Throw "Incorrect setting - please use get-logsettingoptions"            
 }            
             
 $reg = [wmiclass]"\\$computer\root\default:StdRegprov"            
            
 $key = "SYSTEM\CurrentControlSet\Services\NTDS\Diagnostics"            
             
 switch ($computer){            
   "."         {$computername = $env:COMPUTERNAME}            
   "localhost" {$computername = $env:COMPUTERNAME}            
   default     {$computername = $computer}            
 }            
             
 $result = $reg.SetDwordValue($HKLM, $key, $setting, $level)  ## REG_DWORD            
            
}#process             
END{}#end            
            
<# 
.SYNOPSIS
Sets AD diagnostic logging levels 

.DESCRIPTION
Sets AD diagnostic logging levels 

.PARAMETER  Computer
Computer Name

.PARAMETER  Setting
The setting to be logged

.PARAMETER  Level
The level of logging to be applied

.EXAMPLE
set-logsetting -computer server02 -setting "5 Replication Events" -level 1

.EXAMPLE
get-logsettingoptions | 
foreach {set-logsetting -computer server02 -setting $_ -level 2}

.EXAMPLE
get-logsettingoptions | 
where {$_ -like "19*"} | 
foreach {set-logsetting -computer server02 -setting $_ -level 5}

.LINK

http://support.microsoft.com/kb/314980

#>            
            
}


The setting and level are input as parameters. The level can be checked as a range – the setting to log is checked against the hash table of logging types. The WMI SetDWord method is used to perform the change. A few of the ways the setting to use can be input are shown in the help examples.

Leave a Reply