Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

backing up gpo dns and dhcp with output

Posted on 2014-09-03
13
Medium Priority
?
345 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 11

Expert Comment

by:Joe Klimis
ID: 40361071
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
ID: 40373135
thanks joe

testing now...
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 40379506
how are you getting on ?
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

Author Comment

by:ranski
ID: 40382065
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 11

Expert Comment

by:Joe Klimis
ID: 40382252
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
ID: 40382419
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
 

Author Comment

by:ranski
ID: 40384816
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 11

Expert Comment

by:Joe Klimis
ID: 40385458
I will check this for you tomorrow.
0
 

Author Comment

by:ranski
ID: 40406609
any updates joe?
0
 
LVL 11

Expert Comment

by:Joe Klimis
ID: 40408625
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
ID: 40434546
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 11

Accepted Solution

by:
Joe Klimis earned 2000 total points
ID: 40434822
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
ID: 40435377
nice persistence.... have some points you have earned them  :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
"Any files you do not have backed up in at least two [other] places are files you do not care about."
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…

824 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