AD scripts

AD serves as a centralized security management solution that houses all network resources. The purpose of Active Directory is to enable organizations to keep their network secure and organized without having to use up excessive IT resources.

Rakuten provides solutions to customers to handle the performance count of the various events, faults, cache bytes and so on in the Active Directory services by using the Active directory agent.

You can use any powershell script which returns json as a result. Users will view these results in the Analytics page and can better the performance of memory usage, cpu, how system behaves if more users are connected with Active directory and so on. This in turn helps in managing costs.

Some AD script examples are given below.

AD Script Example 1

$results = New-Object -TypeName PSCustomObject
# Add HostName, Timestamp, and Source properties
$results | Add-Member -MemberType NoteProperty -Name "HostName" -Value $env:COMPUTERNAME
$results | Add-Member -MemberType NoteProperty -Name "Timestamp" -Value (Get-Date -Format "yyyy-MM-dd HH:mm:ss")
$results | Add-Member -MemberType NoteProperty -Name "Source" -Value "AD.ps1"
# Initialize ErrorException property
$errorException = $null
# Define counters
$counters = @(
"\Memory\Cache Faults/sec",
"\Memory\Cache Bytes",
"\Memory\Cache Bytes Peak",
"\Memory\Long-Term Average Standby Cache Lifetime (s)",
"\NTDS\LDAP Client Sessions",
"\NTDS\LDAP Searches/sec",
"\NTDS\LDAP Successful Binds/sec",
"\System\Context Switches/sec",
"\System\Processor Queue Length",
"\ADWS\Allocated Connections",
"\ADWS\Possible Connections",
"\ADWS\Number of Directory Instances",
"\ADWS\Custom Action LDAP Cache Maximum Possible Size",
"\ADWS\Custom Action DS RPC Cache Maximum Possible Size",
"\NTDS\LDAP Outbound Bytes",
"\NTDS\LDAP Outbound Bytes/sec",
"\NTDS\Links added",
"\NTDS\ATQ Threads Total",
"\NTDS\Approximate highest DNT",
"\NTDS\DRA Highest USN Committed (Low part)",
"\NTDS\DRA Highest USN Issued (Low part)",
"\NTDS\DS % Reads from KCC",
"\NTDS\DS % Reads from LSA",
"\NTDS\DS % Reads Other",
"\NTDS\DS % Searches from KCC"
)
# Retrieve all counters at once
try {
$counterValues = Get-Counter -Counter $counters -MaxSamples 1 -ErrorAction SilentlyContinue
$counterValues.CounterSamples | ForEach-Object {
$name = $_.Path.Split('\')[-1].Replace(" ", "") # Simplify name extraction
$results | Add-Member -MemberType NoteProperty -Name $name -Value $_.CookedValue
}
} catch {
$errorException = $_.Exception.Message
}
# Add ErrorException property
$results | Add-Member -MemberType NoteProperty -Name "Error" -Value $errorException
# Print the results to STDOUT in JSON
$results | ConvertTo-Json

AD Script Example 2

# Custom function to get system memory usage
function Get-MemoryUsage {
$memoryInfo = Get-WmiObject -Class Win32_OperatingSystem
$totalMemory = $memoryInfo.TotalVisibleMemorySize
$freeMemory = $memoryInfo.FreePhysicalMemory
$usedMemory = $totalMemory - $freeMemory
# Return memory usage information
return @{
"totalMemoryMB" = [math]::Round($totalMemory / 1MB, 2)
"freeMemoryMB" = [math]::Round($freeMemory / 1MB, 2)
"usedMemoryMB" = [math]::Round($usedMemory / 1MB, 2)
}
}
try {
# Call the custom function to get memory usage
$memoryUsage = Get-MemoryUsage
# Code block where an exception might occur
$result = 10 / 0 # This will cause a division by zero error
}
catch {
# Catch block to handle the exception
$errorMessage = $_.Exception.Message
}
finally {
# Optional finally block, which executes regardless of whether an exception occurred or not
$completionMessage = "Script execution complete."
}
# Convert results to JSON format
$jsonResult = @{
"memoryUsage" = $memoryUsage
"errorMessage" = $errorMessage
"completionMessage" = $completionMessage
} | ConvertTo-Json
# Ou

AD Script Example 3

$results = New-Object -TypeName PSCustomObject
# Add HostName property
$hostName = $env:COMPUTERNAME
$results | Add-Member -MemberType NoteProperty -Name "HostName" -Value $hostName
# Add Timestamp property
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$results | Add-Member -MemberType NoteProperty -Name "Timestamp" -Value $timestamp
# Add Source property
$results | Add-Member -MemberType NoteProperty -Name "Source" -Value "NewFile.ps1"
# Initialize ErrorException property
$errorException = $null
$counters = @(
[PSCustomObject]@{ counterName = "Faults"; counterPath = "\Memory\Cache Faults/sec" }
[PSCustomObject]@{ counterName = "Cache Bytes"; counterPath = "\Memory\Cache Bytes" }
[PSCustomObject]@{ counterName = "Cache Bytes Peak"; counterPath = "\Memory\Cache Bytes Peak" }
[PSCustomObject]@{ counterName = "Long Term"; counterPath = "\Memory\Long-Term Average Standby Cache Lifetime (s)" }
)
# Iterate through our target counters and grab our results
foreach ($c in $counters) {
try {
$counterValue = (Get-Counter -MaxSamples 1 -Counter $c.counterPath | Select-Object -ExpandProperty CounterSamples).CookedValue
$results | Add-Member -MemberType NoteProperty -Name $c.CounterName -Value $counterValue
} catch {
$errorException = $_.Exception.Message
}
}
# Add ErrorException property
$results | Add-Member -MemberType NoteProperty -Name "Error" -Value $errorException
# Print the results to STDOUT in JSON for Flex to pick up
$results | ConvertTo-Json