mastering-active-directory-management-with-powershell
mastering-active-directory-management-with-powershell
$DaysInactive = 30
$time = (Get-Date).Adddays(-($DaysInactive))
Get-ADUser -Filter {LastLogonTimeStamp -gt $time -and enabled -eq $true}
-Properties LastLogonTimeStamp | select-object Name,@{Name="Stamp";
Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp).-
ToString('yyyy-MM-dd_hh:mm:ss')}} | export-csv C:\Scripts\activeusers.csv
-notypeinformation
1
Spotting Accounts Configured with
Reversible Encryption
There are several dozen settings within Active Directory that if used can
weaken security and open your environment to the threat of compromise.
The Store password using reversible encryption option is one of those
settings.
2
Import-Module ActiveDirectory
# Array for report.
$report = @()
$schemaIDGUID = @{}
# ignore duplicate errors if any #
$ErrorActionPreference = 'SilentlyContinue'
Get-ADObject -SearchBase (Get-ADRootDSE).schemaNamingContext
-LDAPFilter '(schemaIDGUID=*)' -Properties name, schemaIDGUID |
ForEach-Object
{$schemaIDGUID.add([System.GUID]$_.schemaIDGUID,$_.name)}
Get-ADObject -SearchBase
"CN=Extended-Rights,$((Get-ADRootDSE).configurationNamingContext)"
-LDAPFilter '(objectClass=controlAccessRight)' -Properties name, rightsGUID |
ForEach-Object {$schemaIDGUID.add([System.GUID]$_.rightsGUID,$_.name)}
$ErrorActionPreference = 'Continue'
# Get a list of AD objects.
$AOs = @(Get-ADDomain | Select-Object -ExpandProperty
DistinguishedName)
$AOs += Get-ADOrganizationalUnit -Filter * | Select-Object -ExpandProperty
DistinguishedName
$AOs += Get-ADObject -SearchBase (Get-ADDomain).DistinguishedName
-SearchScope Subtree -LDAPFilter '(objectClass=*)' | Select-Object
-ExpandProperty DistinguishedName
# Loop through each of the AD objects and retrieve their permissions.
# Add report columns to contain the path.
ForEach ($AO in $AOs) {
$report += Get-Acl -Path "AD:\$AO" |
Select-Object -ExpandProperty Access |
Select-Object @{name='organizationalunit';expression={$AO}}, `
@{name='objectTypeName';expression={if
($_.objectType.ToString() -eq
'00000000-0000-0000-0000-000000000000') {'All'} Else
{$schemaIDGUID.Item($_.objectType)}}}, `
@{name='inheritedObjectTypeName';expression={$schemaIDGUID.Item($_.inhe
ritedObjectType)}}, `
3
Managing Domain Password Policy
The Domain Password policy determines how passwords are created and
how often they need to be changed, etc. The AD default password minimum
is 7 characters. Most AD environments we see have this set to between 8
and 10. Some are set to 0 or 3 or 5.
Password Spray attacks are effective against Active Directory due to bad
passwords (often with short minimum requirements).Increasing password
length can limit Password Spray effectiveness.
Use the following PowerShell Scripts to modify the default password policy
for an Active Directory domain:
Set-ADDefaultDomainPasswordPolicy
[-WhatIf]
[-Confirm]
[-AuthType <ADAuthType>]
[-ComplexityEnabled <Boolean>]
[-Credential <PSCredential>]
[-Identity] <ADDefaultDomainPasswordPolicy>
[-LockoutDuration <TimeSpan>]
[-LockoutObservationWindow <TimeSpan>]
[-LockoutThreshold <Int32>]
[-MaxPasswordAge <TimeSpan>]
[-MinPasswordAge <TimeSpan>]
[-MinPasswordLength <Int32>]
[-PassThru]
[-PasswordHistoryCount <Int32>]
[-ReversibleEncryptionEnabled <Boolean>]
[-Server <String>]
[<CommonParameters>]
4
You can also use the Windows Server Backup module to backup Active
Directory on a domain controller with PowerShell. The following PowerShell
script will backup server’s System State to the specified drive:
$WBpolicy = New-WBPolicy
Final Thoughts
Administrators have a tough, often tedious role to fill. Even relatively simple security
tasks, like deleting inactive users or ensuring that the devices on the network are all
patched and operating optimally can be very time-consuming when you factor in the
sheer number of devices being managed. However, with the help of such PowerShell
scripts, IT admins can simplify common IT tasks and automate them whenever necessary.