Skip to content

Instantly share code, notes, and snippets.

@JaekelEDV
Created February 13, 2018 15:31
Show Gist options
  • Save JaekelEDV/df821bd09a96eaba5786c200d9d9506c to your computer and use it in GitHub Desktop.
Save JaekelEDV/df821bd09a96eaba5786c200d9d9506c to your computer and use it in GitHub Desktop.
Powershell Script to create daily scheduled task for AD snapshots
#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