Created
February 13, 2018 15:31
Powershell Script to create daily scheduled task for AD snapshots
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
#region Check if System is DC and logged-on user is admin | |
$DomainRole = Get-WmiObject -Class Win32_ComputerSystem | Select-Object -ExpandProperty DomainRole | |
if( $DomainRole -match '4|5' ) | |
#0=StandaloneWorkstation, 1=MemberWorkstation, 2=StandaloneServer, 3=MemberServer, 4=BackupDC, 5=PrimaryDC | |
{Write-Host 'Check: Machine is DC' -ForegroundColor Green} | |
else | |
{Write-Host 'Oops: This Script must be run on a DC' -ForegroundColor Red -InformationAction Stop} | |
if (([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) | |
{Write-Host 'Check: You are Admin and good to go...' -ForegroundColor Green | |
} | |
else | |
{Write-Host 'Oops: Please run Powershell as Administrator to complete this task!' -ForegroundColor Red | |
} | |
#endregion | |
#region Create Scheduled Task | |
#&: call operator; tells PowerShell to interpret the string that follows as an executable command | |
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument {-noexit -command (&"ntdsutil.exe 'activate instance ntds' snapshot create quit quit")} | |
$trigger = New-ScheduledTaskTrigger -Daily -At 03:00 | |
$principal = New-ScheduledTaskPrincipal -UserId 'NT Authority\System' -LogonType Password | |
Register-ScheduledTask -TaskName ADSnapshot -Action $action -Trigger $trigger -Principal $principal | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment