Created
April 15, 2014 12:48
-
-
Save janikvonrotz/10729700 to your computer and use it in GitHub Desktop.
PowerShell: Backup Active Directory#PowerShell#ActiveDirectory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
$Metadata = @{ | |
Title = "Backup ActiveDirecotry" | |
Filename = "Backup-ActiveDirectory.ps1" | |
Description = "" | |
Tags = "backup, active, directory, ntsutil" | |
Project = "" | |
Author = "Janik von Rotz" | |
AuthorContact = "http://janikvonrotz.ch" | |
CreateDate = "2014-04-15" | |
LastEditDate = "2014-04-15" | |
Url = "" | |
Version = "0.0.0" | |
License = @' | |
This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Switzerland License. | |
To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ch/ or | |
send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA. | |
'@ | |
} | |
#> | |
try{ | |
<# | |
#--------------------------------------------------# | |
# about | |
#--------------------------------------------------# | |
The restore and backup process is described here: http://technet.microsoft.com/en-us/library/dd581644(WS.10).aspx | |
#> | |
#--------------------------------------------------# | |
# settings | |
#--------------------------------------------------# | |
$Path = "C:\backup\ActiveDirectory" | |
#--------------------------------------------------# | |
# main | |
#--------------------------------------------------# | |
# create backup file name | |
$Filename = "ADBackupFull" + "#" + $((Get-Date -Format s) -replace ":","-") + ".bak" | |
$Filepath = Join-Path $Path $Filename | |
# backup active directory | |
Invoke-Expression 'ntdsutil "activate instance ntds" ifm "create full $Filepath" quit quit' | |
# get dates for backup retention exclusion | |
$Today = Get-Date -Format d | |
$FirstDateOfWeek = Get-Date (Get-Date).AddDays(-[int](Get-Date).Dayofweek) -Format d | |
$FirstDateOfMonth = Get-Date -Day 1 -Format d | |
# delete all backups except for today, first day of week and first day of month | |
Get-ChildItem $Path | select *,@{L="CreationTimeDate";E={Get-Date $_.CreationTime -Format d}} | Group-Object CreationTimeDate | %{ | |
# only one backup per day | |
if($_.Count -gt 1){ | |
$_.Group | Sort-Object CreationTime -Descending | Select-Object -Skip 1 | |
} | |
# keep only required backups | |
$_.Group | Where-Object{$_.CreationTimeDate -ne $Today -and $_.CreationTimeDate -ne $FirstDateOfWeek -and $_.CreationTimeDate -ne $FirstDateOfMonth} | |
} | Remove-Item -Recurse -Force | |
}catch{ | |
Write-PPErrorEventLog -Source "Backup ActiveDirectory" -ClearErrorVariable | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-16"?> | |
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> | |
<RegistrationInfo> | |
<Date>2013-10-07T09:08:50.3896932</Date> | |
<Author>Janik von Rotz(http://janikvonrotz.ch)</Author> | |
<Description>Backup ActiveDirectory</Description> | |
</RegistrationInfo> | |
<Triggers> | |
<CalendarTrigger> | |
<StartBoundary>2013-01-01T04:30:00</StartBoundary> | |
<Enabled>true</Enabled> | |
<ScheduleByDay> | |
<DaysInterval>1</DaysInterval> | |
</ScheduleByDay> | |
</CalendarTrigger> | |
</Triggers> | |
<Settings> | |
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> | |
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> | |
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> | |
<AllowHardTerminate>true</AllowHardTerminate> | |
<StartWhenAvailable>false</StartWhenAvailable> | |
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> | |
<IdleSettings> | |
<StopOnIdleEnd>true</StopOnIdleEnd> | |
<RestartOnIdle>false</RestartOnIdle> | |
</IdleSettings> | |
<AllowStartOnDemand>true</AllowStartOnDemand> | |
<Enabled>true</Enabled> | |
<Hidden>false</Hidden> | |
<RunOnlyIfIdle>false</RunOnlyIfIdle> | |
<WakeToRun>false</WakeToRun> | |
<ExecutionTimeLimit>P1D</ExecutionTimeLimit> | |
<Priority>7</Priority> | |
</Settings> | |
<Actions Context="Author"> | |
<Exec> | |
<Command>$PSapps.PowerShell</Command> | |
<Arguments>$(Get-ChildItem -Path $PSscripts.Path -Filter "Backup-ActiveDirectory.ps1" -Recurse).Fullname</Arguments> | |
<WorkingDirectory>$PSProfile.Path</WorkingDirectory> | |
</Exec> | |
</Actions> | |
</Task> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment