burmat / nathan burchfield
  • security and systems administration
  • security / hacking
    • Domain Enumeration + Exploitation
      • Command and Control
      • Credential Access
      • Defense Evasion
      • Discovery
      • Execution
      • Impact
      • Lateral Movement
      • Persistence
      • Privilege Escalation
    • Tools and Services
      • Adobe Experience Manager (AEM)
      • amass
      • ike-scan
      • jq
      • Shodan
      • smbmap
      • tmux
      • tshark
      • Voice Over IP (VoIP)
    • One-Liners and Dirty Scripts
    • MSFvenom Cheetsheet
    • Web Application Hacking
      • Cross-Site Scripting (XXS)
      • SQL Injection (SQLi)
    • OSCP / PWK - Random Tips and Tricks
  • systems administration
    • Active Directory Administration
    • Exchange Administration
    • System Fixes
    • Helper Commands
    • Log Parsing
    • SQL Server Administration
    • Windows Terminal Themes
Powered by GitBook
On this page
  • Get Active Win10 Machine Patch Level (Last Logon in 60 Days)
  • Get Hosts Last Logon
  • Get Users Last Logon
  • Get Stale Hosts
  • Move Object to Retire OU
  • Disable Everything in OU
  • FILE SYSTEM ADMINISTRATION
  • Getting Directory Sizes
  • Tail a File
  • MISC CLEANUP / MANAGEMENT
  • Clear Cached (MsCacheV2) Credentials
  1. systems administration

Active Directory Administration

This page is dedicated to any and all Active Directory administration

Get Active Win10 Machine Patch Level (Last Logon in 60 Days)

$LastLogon = (Get-Date).Adddays( -(60) ); $Workstations = Get-ADComputer -Filter { LastLogonTimeStamp -gt $LastLogon -and OperatingSystem -like 'Windows 10'} -Properties *; $Workstations = $Workstations | Select-Object -Property DNSHostname,OperatingSystem,OperatingSystemVersion,IPv4Address,LastLogonDate,DistinguishedName,SID; Export-Results -Output $Workstations -Path "C:\Users\burmat\Desktop\Workstations.csv"

Get Hosts Last Logon

Iterate all computer objects in a given domain and get the date/time for the last time they were logged into:

Import-Module ActiveDirectory

function Get-ADHostsLastLogon() {

    $hnames = Get-ADComputer -Filter 'ObjectClass -eq "Computer"' | Select -Expand Name

    foreach ($hname in $hnames) {
        $dcs = Get-ADDomainController -Filter {Name -like "*"}
        $time = 0
        foreach($dc in $dcs) { 
            $computer = Get-ADComputer $hname | Get-ADObject -Properties lastLogon 
            if($computer.LastLogon -gt $time) {
                $time = $computer.LastLogon
            }
        }
        
        $dt = [DateTime]::FromFileTime($time).ToString('g')
        # 12/31/1600 will result if $time = 0 (never logged on before)
        Write-Host $dt", " $hname
    }
    Write-Host "Done."
}

Get-ADHostsLastLogon

Get Users Last Logon

To iterate all user objects in AD and get their last logon time, use:

Import-Module ActiveDirectory

function Get-ADUserLastLogon([string]$userName) {
    $dcs = Get-ADDomainController -Filter {Name -like "*"}
    $time = 0
    foreach($dc in $dcs) { 
        $hostname = $dc.HostName
        $user = Get-ADUser $userName | Get-ADObject -Properties lastLogon 
        if($user.LastLogon -gt $time) {
            $time = $user.LastLogon
        }
    }
    $dt = [DateTime]::FromFileTime($time)
    Write-Host $username "last logged on at:" $dt 
}
$unames = Get-ADUser -Filter 'ObjectClass -eq "User"' | Select -Expand SamAccountName
foreach ($uname in $unames) { Get-ADUserLastLogon($uname); } 

Get Stale Hosts

Use the following to generate a list of hosts that have not been logged into for the past 30 days:

Import-Module ActiveDirectory

function Get-StaleComputers() {
    $time = (Get-Date).Adddays(-30)
    Get-ADComputer -Filter { LastLogonTimeStamp -lt $time } -Properties LastLogonTimeStamp | Select-Object Name,@{Name="Stamp"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}} # | Export-CSV C:\temp\unused_machines.csv -notypeinformation
    Write-Host done.
}

Get-StaleComputers

Move Object to Retire OU

# to move a user:
 Get-ADUser $uname | Move-ADObject -TargetPath 'OU=Retire,DC=burmat,DC=co' 
 
# to move a computer:
 Get-ADComputer $hname | Move-ADObject -TargetPath 'OU=Retire,DC=burmat,DC=co' 

Disable Everything in OU

Every few weeks, I run the following (as Domain Admin) to ensure the OU I use for my "Recycle Bin" is filled with only disabled accounts: Get-ADUser -Filter * -SearchBase 'OU=Retire,DC=burmat,DC=co' | Disable-ADAccount

FILE SYSTEM ADMINISTRATION

Getting Directory Sizes

I use the following command to generate a list of user profile's on a file server. It is useful to keep track of users that are exceeding our expectations when it comes to consuming space on a global server:

Get-ChildItem | Where-Object { $.PSIsContainer } | ForEach-Object { $.Name + ": " + "{0:N2}" -f ((Get-ChildItem $_ -Recurse | Measure-Object Length -Sum -ErrorAction SilentlyContinue).Sum / 1MB) + " MB" }

Tail a File

Similar to tail -f filename, you can use Get-Content to watch a file for changes:

Get-Content -Path "\\server\logs\prod.server.log" -Wait

MISC CLEANUP / MANAGEMENT

Clear Cached (MsCacheV2) Credentials

A domain-joined endpoint that is taken from the domain might still have cached (mscachev2) domain logins residing on it. This is why I always wipe the system or use the following to remove any cached credentials:

Run regedit and give your current local account Write access to the "SECURITY" node. After restarting regedit, navigate to: HKEY_LOCAL_MACHINE\Security\Cache

Cached credentials are stored in the binary values of NL$1 through NL$10. Zeroing out these values will clear the cached entries. Delete them if you want to remove them and disable this feature completely.

PreviousOSCP / PWK - Random Tips and TricksNextExchange Administration

Last updated 7 months ago

(Find my most recent copy on )

(Find my most recent copy on )

(Find my most recent copy on )

I like to use the scripts above ( and ) to automatically move objects into the "Retire" OU using the following command(s):

It's now trivial to .

my GitHub
my GitHub
my GitHub
Get Hosts Last Logon
Get Users Last Logon
disable all objects in the given OU