Tuesday, April 21, 2015

Active Directory ntds.dit database file size checking

This script will check the dit size, dit freespace and the drive size/freespace information on the disk that is hosting the file. It assumes that garbage collection debug logging is turned on, so that 1646 events can be read to find out how much free space is in the DIT file on each machine.

import-module activedirectory
$domaindetail = get-addomain
$domaincontrollers = $domaindetail.ReplicaDirectoryServers + $domaindetail.ReadOnlyReplicaDirectoryServers
out-default -inp ("Computername,DITsizeGB,FreespaceGB,DBDriveSize,DBDriveFreespace")
foreach ($dc in $domaincontrollers) {
 #assumes that NTDS diags for Garbage collections are set to 1
 try {
 $DITfreespace = get-winevent -FilterHashtable @{
  ID=1646;logname="Directory Service"
 } -max 1 -computername $dc |
  select -exp properties
 #value from the event log is in mb, not bytes, so we divide by kb to get gb
 $DITfreespace = $DITfreespace[0].value /1kb
 $DITfreespace = "{0:N3}" -f $DITfreespace

 #get DB location
 $RegObj = [microsoft.win32.registrykey]::openremotebasekey('LocalMachine',$dc)
 $regkey = $regobj.opensubkey("SYSTEM\\CurrentControlSet\\Services\\NTDS\\Parameters")
 $val = $regkey.getvalue("DSA Database file")
 $driveletter = $val[0] + $val[1]
 $val= $val.replace(":","$")
 $val = "\\" + $dc + "\" + $val
 #get size of dit
 $len = (dir \\$dc\e$\ntds\ntds.dit|select length).length /1gb
 $len = "{0:N2}" -f $len

 #get drive details
 $driveinformation = Get-WmiObject Win32_logicaldisk  -computer $dc -filter ("DeviceID = '" + $driveletter + "'")|select size,freespace
 $size = "{0:N2}" -f ($driveinformation.size /1gb)
 $freespace = "{0:N2}" -f ($driveinformation.freespace /1gb)
 out-default -inp ("$dc,$len,$DITfreespace,$size,$freespace")

 } catch {
  out-default -inp ("$dc,Failedtoconnect")

No comments:

Post a Comment