Restoring Active Directory Attributes with PowerShell - Part 3

Welcome to the third and final article of the Restoring Active Directory Attributes with PowerShell. Today we are going to create a scheduled task in order to execute a PowerShell script that will manage our Active Directory snapshots.

First, we are going to create the script that will manage the snapshots but in order to do that we have to decide on how often and how many snapshots we want to have. Personally, I go for two snapshots per day for a period of three days, nut I have these snapshots spread across multiple Domain Controllers.

The following script will create an Active Directory snapshot and then remove all the older snapshots keeping a total of 3. That way, when it is executed on a daily basis, you will have three snapshots for the last 3 three days.

# Create the new Active Directory snapshot

# Get the current date
$now = [DateTime]::Now

# Remove snapshots older than three days from now
Get-ActiveDirectorySnapshot |
    Where-Object {$_.Date -lt $now.AddDays(-3)} |
        Remove-ActiveDirectorySnapshot -Confirm:$false

Let's talk about the commands. First we create an Active Directory snapshot and then we get the current date in order to filter the snapshots later on. We compare the date of each snapshot to the current date minus three days and the snapshots that are older than three days are removed.

The following commands will create the scheduled task for you, you just have to update the location of your script and place the CPolydorou.ActiveDirectory module in a folder that is in the PowerShell module path for the System account.

$action = New-ScheduledTaskAction -Execute C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe `
                                  -Argument C:\Automation\ActiveDirectorySnapshot.ps1

$trigger = New-ScheduledTaskTrigger -Daily -At 08AM

$principal = New-ScheduledTaskPrincipal -LogonType S4U `
                                        -UserId "SYSTEM" `
                                        -RunLevel Highest

Register-ScheduledTask -TaskName "Active Directory Snapshot Management" `
                       -Action $action `
                       -Trigger $trigger `
                       -Principal $principal `
                       -Description "Manage Active Directory Snapshots" 

I would recommend creating a service account in order to execute the task and create the snapshots.

At the end of the last article I would like to stress out the importance of having Active Directory snapshots. With snapshots, you do not solely rely on your backup software in order to restore a user to a former state and you do not have to wait for the backup team or procedure.

Schedule your Active Directory snapshots NOW. Create an Active Directory Snapshot before any massive changes. Be the one that will remedy a major Active Directory incident within seconds!

Popular posts from this blog

Syslog Message Collection for OMS from sources that do not support the agent

How to Configure Message Forwarding on a Mailbox Level

IIS Client Certificate Revocation Check Disable