Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

backing up gpo dns and dhcp with output

Posted on 2014-09-03
13
311 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
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 10

Expert Comment

by:JoeKlimis
ID: 40379506
how are you getting on ?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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 10

Expert Comment

by:JoeKlimis
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 10

Expert Comment

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

Author Comment

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

Expert Comment

by:JoeKlimis
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 10

Accepted Solution

by:
JoeKlimis earned 500 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
In this Micro Tutorial viewers will learn how to restore single file or folder from Bare Metal backup image of their system. Tutorial shows how to restore files and folders from system backup. Often it is not needed to restore entire system when onl…
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…

808 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