Solved

backing up gpo dns and dhcp with output

Posted on 2014-09-03
13
289 Views
Last Modified: 2014-11-11
hello

trying to create a script that will backup all gpo, DNS zones, and DHCP config file using powershell and then output the success or failure of the backup to an email. i can do the backup-gpo, export-dnsserverzone, backup-dhcpserver and send-mailmessage commands i just would like some ideas on the best way to do a validation against each to make sure it has run successfully. google isnt really too helpful unless you are a proper scripter! thanks so much
0
Comment
Question by:ranski
  • 7
  • 6
13 Comments
 
LVL 10

Expert Comment

by:JoeKlimis
Comment Utility
Hi

I have not had a chanceto test this  , but the following should be a good starting point

Please let me know if this meets your requirements.

Regards

Joe

(param [String]$server=localhost)
$datedFolder = Get-Date -Format yyyy.MM.dd
$backupLocation = "c:\backup"
New-Item -Path "$backupLocation\DHCP\$newfolder" -Type Directory
#DHCP Backup
Invoke-Expression "netsh dhcp server export D:\backup\DHCP\$datedFolder\dhcp.xml all"
Invoke-Expression "netsh dhcp server backup D:\backup\DHCP\$datedFolder"



#### backup gpo 

# GPOBackupSamp.PS1  
# Script By: Tim B. 
# This script Backup all GPOs and save it to a folder named as the current date. 
# Change the Path "\\server\c$\Backup\GroupPolicies\$date" to your server path 
# ref http://gallery.technet.microsoft.com/scriptcenter/Backup-All-GPOs-Powershell-bcdb7b5e
#
Import-Module grouppolicy 
$date = get-date -format M.d.yyyy 
New-Item -Path "$backuplocation\GroupPolicies\$datedFolder" -ItemType directory 
$cmd  = "Backup-Gpo -All -Path ""$backuplocation\GroupPolicies\$datedFolder"" " 
Invoke-Expression $cmd



#——————————————————————————————–#
# Script_Name : DNS_Backup.ps1
# Description : backup all DNS Zones defined on a Windows 2008 DNS Server
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.4 - Intergrated comments from Jeffrey Hicks
# Date : October 2011
# Created by Griffon
#  ref site http://c-nergy.be/blog/?p=1837
#——————————————————————————————-#

#– DEFINE VARIABLE——#
# Get Name of the server with env variable
$DNSSERVER= $server#—Define folder where to store backup  —–#
$BkfFolder="$backuplocation\DNS\$datedFolder" 
#—Define file name where to store Dns Settings
$StrFile=Join-Path $BkfFolder "input.csv"
#—-Check if folder exists. if exists, delete contents–#
if (-not(test-path $BkfFolder)) {
new-item $BkfFolder -Type Directory | Out-Null
} else {

Remove-Item $BkfFolder"\*" -recurse
}

#—- GET DNS SETTINGS USING WMI OBJECT ——–#
#– Line wrapped should be only one line –#
$List = get-WmiObject -ComputerName $DNSSERVER
-Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone

#—-Export information into input.csv file —#
#– Line wrapped should be only one line –#
$list | Select Name,ZoneType,AllowUpdate,@{Name="MasterServers";Expression={$_.MasterServers}},
DsIntegrated | Export-csv $strFile -NoTypeInformation

#— Call Dnscmd.exe to export dns zones
$list | foreach {
$path="backup\"+$_.name
$cmd="dnscmd {0} /ZoneExport {1} {2}" -f $DNSSERVER,$_.Name,$path
Invoke-Expression $cmd
}
# End of dns backup
#——————————————————————————————-#

Open in new window

0
 

Author Comment

by:ranski
Comment Utility
thanks joe

testing now...
0
 
LVL 10

Expert Comment

by:JoeKlimis
Comment Utility
how are you getting on ?
0
 

Author Comment

by:ranski
Comment Utility
hey joe

get this error when running it

param : The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\backup\DHCP_DNS_GPO_Daily_Backup.ps1:1 char:2
+ (param [String]$server=localhost)
+  ~~~~~
    + CategoryInfo          : ObjectNotFound: (param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
 
New-Item : The network name cannot be found.
At C:\backup\DHCP_DNS_GPO_Daily_Backup.ps1:4 char:1
+ New-Item -Path "$backupLocation\DHCP\$newfolder" -Type Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (\\backup$\DHCP\:String) [New-Item], IOException
    + FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

Open in new window

0
 
LVL 10

Expert Comment

by:JoeKlimis
Comment Utility
change line 1 to

$server="localhost"

and line 4 to
New-Item -Path "$backupLocation\DHCP\$datedfolder" -Type Directory

regards

Joe
0
 

Author Comment

by:ranski
Comment Utility
hey joe heres the code

(param [String]$server=localhost)
$datedFolder = Get-Date -Format dd.MM.yyyy
$backupLocation = "\\##########\backup$"
New-Item -Path "$backupLocation\DHCP\$newfolder" -Type Directory
#DHCP Backup
Invoke-Expression "netsh dhcp server export C:\backup\DHCP\$datedFolder\dhcp.xml all"
Invoke-Expression "netsh dhcp server backup C:\backup\DHCP\$datedFolder"



#### backup gpo 

# GPOBackupSamp.PS1  
# Script By: Tim B. 
# This script Backup all GPOs and save it to a folder named as the current date. 
# Change the Path "\\server\c$\Backup\GroupPolicies\$date" to your server path 
# ref http://gallery.technet.microsoft.com/scriptcenter/Backup-All-GPOs-Powershell-bcdb7b5e
#
Import-Module grouppolicy 
$date = get-date -format M.d.yyyy 
New-Item -Path "$backuplocation\GPO\$datedFolder" -ItemType directory 
$cmd  = "Backup-Gpo -All -Path ""$backuplocation\GPO\$datedFolder"" " 
Invoke-Expression $cmd



#——————————————————————————————–#
# Script_Name : DNS_Backup.ps1
# Description : backup all DNS Zones defined on a Windows 2008 DNS Server
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.4 - Intergrated comments from Jeffrey Hicks
# Date : October 2011
# Created by Griffon
#  ref site http://c-nergy.be/blog/?p=1837
#——————————————————————————————-#

#– DEFINE VARIABLE——#
# Get Name of the server with env variable
$DNSSERVER= $server#—Define folder where to store backup  —–#
$BkfFolder="$backuplocation\DNS\$datedFolder" 
#—Define file name where to store Dns Settings
$StrFile=Join-Path $BkfFolder "input.csv"
#—-Check if folder exists. if exists, delete contents–#
if (-not(test-path $BkfFolder)) {
new-item $BkfFolder -Type Directory | Out-Null
} else {

Remove-Item $BkfFolder"\*" -recurse
}

#—- GET DNS SETTINGS USING WMI OBJECT ——–#
#– Line wrapped should be only one line –#
$List = get-WmiObject -ComputerName $DNSSERVER
-Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone

#—-Export information into input.csv file —#
#– Line wrapped should be only one line –#
$list | Select Name,ZoneType,AllowUpdate,@{Name="MasterServers";Expression={$_.MasterServers}},
DsIntegrated | Export-csv $strFile -NoTypeInformation

#— Call Dnscmd.exe to export dns zones
$list | foreach {
$path="backup\"+$_.name
$cmd="dnscmd {0} /ZoneExport {1} {2}" -f $DNSSERVER,$_.Name,$path
Invoke-Expression $cmd
}
# End of dns backup
#——————————————————————————————-#

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:ranski
Comment Utility
ok got it working up until dns. its asking me for a class for the get-wmiobject call.
shouldnt this automatically know where to go? server 2012
thanks!
0
 
LVL 10

Expert Comment

by:JoeKlimis
Comment Utility
I will check this for you tomorrow.
0
 

Author Comment

by:ranski
Comment Utility
any updates joe?
0
 
LVL 10

Expert Comment

by:JoeKlimis
Comment Utility
Hi
Sorry for the delay , I had a holiday from my computer ;-)

Please try the following
# 
param([String]$server="localhost")
$datedFolder = Get-Date -Format dd.MM.yyyy
$backupLocation = "\\$server\backup"
$folder = "$backupLocation\DHCP\$datedFolder"
if (-not(test-path $folder )) {New-Item -Path "$backupLocation\DHCP\$datedFolder" -Type Directory}
#DHCP Backup
Invoke-Expression "netsh dhcp server export $backupLocation\DHCP\$datedFolder\dhcp.xml all"
Invoke-Expression "netsh dhcp server backup $backupLocation\dhcp\$datedFolder"



#### backup gpo 

# GPOBackupSamp.PS1  
# Script By: Tim B. 
# This script Backup all GPOs and save it to a folder named as the current date. 
# Change the Path "\\server\c$\Backup\GroupPolicies\$date" to your server path 
# ref http://gallery.technet.microsoft.com/scriptcenter/Backup-All-GPOs-Powershell-bcdb7b5e
#
Import-Module grouppolicy 
$folder = "$backupLocation\gpo\$datedFolder"
if (-not(test-path $folder )) {New-Item -Path "$backupLocation\GPO\$datedFolder" -Type Directory}

$cmd  = "Backup-Gpo -All -Path ""$folder"" " 
Invoke-Expression $cmd



#——————————————————————————————–#
# Script_Name : DNS_Backup.ps1
# Description : backup all DNS Zones defined on a Windows 2008 DNS Server
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.4 - Intergrated comments from Jeffrey Hicks
# Date : October 2011
# Created by Griffon
#  ref site http://c-nergy.be/blog/?p=1837
#——————————————————————————————-#

#– DEFINE VARIABLE——#
# Get Name of the server with env variable
$DNSSERVER= $server  #—Define folder where to store backup  —–#
$BkfFolder="$backuplocation\DNS\$datedFolder" 
#—Define file name where to store Dns Settings
$StrFile=Join-Path $BkfFolder "input.csv"
#—-Check if folder exists. if exists, delete contents–#
if (-not(test-path $BkfFolder)) {
new-item $BkfFolder -Type Directory | Out-Null
} else {

Remove-Item $BkfFolder"\*" -recurse
}

#—- GET DNS SETTINGS USING WMI OBJECT ——–#
#– Line wrapped should be only one line –#
$List = get-WmiObject -ComputerName $DNSSERVER -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone

#—-Export information into input.csv file —#
#– Line wrapped should be only one line –#
$list | Select Name,ZoneType,AllowUpdate,@{Name="MasterServers";Expression={$_.MasterServers}},DsIntegrated | Export-csv $strFile -NoTypeInformation

#— Call Dnscmd.exe to export dns zones
 foreach ($item in $list) 
 { $path="backup\"+$item.name 
$cmd="dnscmd {0} /ZoneExport {1} {2}" -f $DNSSERVER,$item.Name,$path
Invoke-Expression $cmd
}
# End of dns backup
#——————————————————————————————-

Open in new window

0
 

Author Comment

by:ranski
Comment Utility
hello joe

sorry for the delay.. tested. the DNS part is working now but it exports the zone files to system32\DNS and doesnt created a dated folder like GPO and DHCP do... had a go at trying to fix it myself but failed :(

thanks
0
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
Comment Utility
Hi

I hadn't noticed that :-O, but its now fixed



#
param([String]$server="localhost")
$datedFolder = Get-Date -Format dd.MM.yyyy
$backupLocation = "\\$server\backup"
$folder = "$backupLocation\DHCP\$datedFolder"
if (-not(test-path $folder )) {New-Item -Path "$backupLocation\DHCP\$datedFolder" -Type Directory}
#DHCP Backup
Invoke-Expression "netsh dhcp server export $backupLocation\DHCP\$datedFolder\dhcp.xml all"
Invoke-Expression "netsh dhcp server backup $backupLocation\dhcp\$datedFolder"



#### backup gpo

# GPOBackupSamp.PS1
# Script By: Tim B.
# This script Backup all GPOs and save it to a folder named as the current date.
# Change the Path "\\server\c$\Backup\GroupPolicies\$date" to your server path
# ref http://gallery.technet.microsoft.com/scriptcenter/Backup-All-GPOs-Powershell-bcdb7b5e
#
Import-Module grouppolicy
$folder = "$backupLocation\gpo\$datedFolder"
if (-not(test-path $folder )) {New-Item -Path "$backupLocation\GPO\$datedFolder" -Type Directory}

$cmd  = "Backup-Gpo -All -Path ""$folder"" "
Invoke-Expression $cmd



#——————————————————————————————–#
# Script_Name : DNS_Backup.ps1
# Description : backup all DNS Zones defined on a Windows 2008 DNS Server
# Requirements : Windows 2008/R2 + DNS Management console Installed
# Version : 0.4 - Intergrated comments from Jeffrey Hicks
# Date : October 2011
# Created by Griffon
#  ref site http://c-nergy.be/blog/?p=1837
#——————————————————————————————-#

#– DEFINE VARIABLE——#
# Get Name of the server with env variable
$DNSSERVER= $server  #—Define folder where to store backup  —–#
$BkfFolder="$backuplocation\DNS\$datedFolder"
#—Define file name where to store Dns Settings
$StrFile=Join-Path $BkfFolder "input.csv"
#—-Check if folder exists. if exists, delete contents–#
if (-not(test-path $BkfFolder)) {
new-item $BkfFolder -Type Directory | Out-Null
} else {
Remove-Item $BkfFolder"\*" -recurse
}

#—- GET DNS SETTINGS USING WMI OBJECT ——–#
#– Line wrapped should be only one line –#
$List = get-WmiObject -ComputerName $DNSSERVER -Namespace root\MicrosoftDNS -Class MicrosoftDNS_Zone

#—-Export information into input.csv file —#
#– Line wrapped should be only one line –#
$list | Select Name,ZoneType,AllowUpdate,@{Name="MasterServers";Expression={$_.MasterServers}},DsIntegrated | Export-csv $strFile -NoTypeInformation

#— Call Dnscmd.exe to export dns zones
foreach ($item in $list)
{ $path="backup\" +$item.name
$cmd="dnscmd {0} /ZoneExport {1} {2}" -f $DNSSERVER,$item.Name,$path
Invoke-Expression $cmd
move-item "c:\windows\system32\dns\backup\$($item.name)" $bkfFolder  
}
# End of dns backup
#——————————————————————————————-

Open in new window

0
 

Author Comment

by:ranski
Comment Utility
nice persistence.... have some points you have earned them  :)
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Are you looking to recover an email message or a contact you just deleted mistakenly? Or you are searching for a contact that you erased from your MS Outlook ‘Contacts’ folder and now realized that it was important.
Synchronize a new Active Directory domain with an existing Office 365 tenant
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now