Link to home
Start Free TrialLog in
Avatar of ranski
ranski

asked on

backing up gpo dns and dhcp with output

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
Avatar of Joe Klimis
Joe Klimis
Flag of United Kingdom of Great Britain and Northern Ireland image

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

Avatar of ranski
ranski

ASKER

thanks joe

testing now...
how are you getting on ?
Avatar of ranski

ASKER

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

change line 1 to

$server="localhost"

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

regards

Joe
Avatar of ranski

ASKER

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

Avatar of ranski

ASKER

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!
I will check this for you tomorrow.
Avatar of ranski

ASKER

any updates joe?
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

Avatar of ranski

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Joe Klimis
Joe Klimis
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ranski

ASKER

nice persistence.... have some points you have earned them  :)