Skip to content

Instantly share code, notes, and snippets.

@gjyoung1974
Last active October 13, 2019 20:40
Show Gist options
  • Save gjyoung1974/aec51db03900ac4c258423ea75f7401d to your computer and use it in GitHub Desktop.
Save gjyoung1974/aec51db03900ac4c258423ea75f7401d to your computer and use it in GitHub Desktop.
#*********************************************************************************************
# Create a folder named C:\Script prior executing the Script and a BATCH file
# Also create a Text file named C:\Script\Script.txt.
# Paste the following command into the text file delete shadows all.
# It will delete all full server backup shadow copies for efficient disk space management.
#*********************************************************************************************
#Backup systemstate and delete all backups except last 4 copies.START
#wbadmin start backup -backuptarget:D: -allCritical -vssfull -quiet
#diskshadow.exe /s C:\Script\Script.txt
#Backup systemstate and delete all backups except last 4 copies.END
#Import required PowerShell Modules
Import-Module ActiveDirectory
Import-Module GroupPolicy
#Backup all Group Policies.START
$Computer = $env:COMPUTERNAME
$date = Get-Date -Format "H.m.d.M.yyyy"
$GPOPath = 'D:\WindowsImageBackup\GPOAll'
$DestGPO = 'D:\WindowsImageBackup\GPOAll\' + $Computer + '-' + $date
$DestDelGPO = 'D:\WindowsImageBackup\GPOAll\*'
Test-Path -Path $GPOPath -PathType Container
if ( -Not (Test-Path $GPOPath))
{
$null = New-Item -Path $GPOPath -ItemType Directory
}
else
{
Write-Host "Path Exists: $GPOPath"
}
New-Item -Path $DestGPO -ItemType Directory
Get-GPO -all | Backup-GPO -path $DestGPO
Get-ChildItem $DestDelGPO | Where-Object {$_.Lastwritetime -lt (date).adddays(-2)} | Remove-Item -force -recurse -Confirm:$false
#Backup all Group Policies.END
# #Backup all Group Policy Links.START
$GPLinkAllPath = 'D:\WindowsImageBackup\GPLinkAll'
$DestGPLinkAllPath = 'D:\WindowsImageBackup\GPLinkAll\' + $Computer + '-' + $date
$DestGPLinkAllDelPath = 'D:\WindowsImageBackup\GPLinkAll\*'
Test-Path -Path $GPLinkAllPath -PathType Container
if ( -Not (Test-Path $GPLinkAllPath))
{
$null = New-Item -Path $GPLinkAllPath -ItemType Directory
}
else
{
Write-Host "Path Exists: $GPLinkAllPath"
}
New-Item -Path $DestGPLinkAllPath -ItemType Directory
Get-ADOrganizationalUnit -Filter 'Name -like "*"' |
foreach-object {(Get-GPInheritance -Target $_.DistinguishedName).GpoLinks} |
export-csv "$DestGPLinkAllPath\GPLinkBackup.csv" -notypeinformation -delimiter ';'
Get-ChildItem $DestGPLinkAllDelPath | Where-Object {$_.Lastwritetime -lt (date).adddays(-5)} | Remove-Item -force -recurse -Confirm:$false
#Backup all Group Policy Links.END
#Backup all Distinguished Name of Objects in the Root Domain.START
$DNFolderPath = 'D:\WindowsImageBackup\DNAll'
$DNFolderDelPath = 'D:\WindowsImageBackup\DNAll\*'
Test-Path -Path $DNFolderPath -PathType Container
if ( -Not (Test-Path $DNFolderPath))
{
$null = New-Item -Path $DNFolderPath -ItemType Directory
}
else
{
Write-Host "Path Exists: $DNFolderPath"
}
$DNSDate = Get-Date -UFormat "%Y%m%d-%H%M%S"
$DNFilePath = 'D:\WindowsImageBackup\DNAll\DNBackup_' + $DNSDate + '.txt'
$DNList_command = 'dsquery * domainroot -scope subtree -attr modifytimestamp distinguishedname -limit 0 >' + $DNFilePath
Invoke-expression $DNList_command
Get-ChildItem $DNFolderDelPath | Where-Object {$_.Lastwritetime -lt (date).adddays(-10)} | Remove-Item -force -recurse -Confirm:$false
# Backup all Distinguished Name of Objects in the Root Domain.END
#Backup DNS.START
$BckPath = 'D:\DNSBackup'
#Get Name of the server with env variable
$DnsServer = $env:computername
if ( -Not (Test-Path $BckPath))
{
$null = New-Item -Path $BckPath -ItemType Directory
}
else
{
Write-Host "Path Exists: $BckPath"
}
#Define file name for Dns Settings
$File = Join-Path $BckPath "input.csv"
#Get DNS settings using WMI
$List = Get-WmiObject -ComputerName $DnsServer -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone
if ($List) {
#Export information into input.csv file
$List | Select-Object Name,ZoneType,AllowUpdate,@{Name="MasterServers";Expression={$_.MasterServers}},DsIntegrated | Export-csv $File -NoTypeInformation
#Call Dnscmd.exe to export dns zones
$List | ForEach-Object {
dnscmd $DnsServer /ZoneExport $_.Name "$($_.Name).$date.dns"
} #end of ForEach-Object
} #end of if ($List)
else {
Write-Error "Failed to obtain DNS WMI object... exiting script"
} #end of else ($List)
# Backup DNS.END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment