Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

backing up gpo dns and dhcp with output

Posted on 2014-09-03
13
Medium Priority
?
339 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
[X]
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
  • 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Are You Ready for GDPR?

With the GDPR deadline set for May 25, 2018, many organizations are ill-prepared due to uncertainty about the criteria for compliance. According to a recent WatchGuard survey, a staggering 37% of respondents don't even know if their organization needs to comply with GDPR. Do you?

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
To efficiently enable the rotation of USB drives for backups, storage pools need to be created. This way no matter which USB drive is installed, the backups will successfully write without any administrative intervention. Multiple USB devices need t…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

661 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