# # p2fCopyRoot.ps1 # <# .SYNOPSIS This script needs to be executed with administrator privilegs. The script exits if no source p2fRoot or no destination p2fRoot are available. The script must be executed on the machine on which p2fServer is running. For event logging the p2fServer is the message source and the id is 5000. - Stop services. - Backup production p2fRoot. - Copy specified containers from source p2fRoot to destination p2fRoot. - Start services. - Remove source root. #> ################################################################################ ## ## Specifiy Parameters ## ################################################################################ # Path to the source root $source = "C:\tmp\p2f\Quelle\p2fRoot" # Path to the destination root $root = 'C:\tmp\p2f\Ziel\p2fRoot' # Path to backup directory $backup="C:\tmp\p2f\Backup" # list of containers to be replaced $containers = "PRC", "OVL", "RES", "ATM", "DAT" # list of services to stop/start # Beware of the order $services = "p2fService", "p2fRouter", "p2fServer" ##################### NO USER MODIFICATIONS BEYOND THIS POINT ################## Set-StrictMode -Version 3 # Administrator credentials required, else abort with error message If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(` [Security.Principal.WindowsBuiltInRole] "Administrator")) { # Use p2fServer as message source Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "The script p2fCopyRoot must run as an Administrator" Exit 1 } # # If no source root is available abort without message # If (-NOT (Test-Path $source)) { Exit 2 } # # If no destination root is available abort with message # If (-NOT (Test-Path $root)) { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType success -EventId 5000 -Message "The destination root is not available" Exit 3 } $p2fError = $false # Remember stopped services $stopservices=@() foreach($service in $services) { Try { $a = Get-Service -ErrorAction SilentlyContinue -Name $service if ($a) { if($a.Status -eq "running") { Stop-Service -ErrorAction Stop -InputObject $a $stopservices += @($service) } } else { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Service \"" + $service + "\" not installed" } } Catch { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Error on stoping service " + $service } } # Backup If (Test-Path $root) { Try { $date = Get-Date -UFormat "%y-%m-%dT%H-%M" $bpath = $backup + "\" + "p2fRoot-" + $date Copy-Item -Recurse -ErrorAction Stop $root $bpath } Catch { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Error on backup root" $p2fError = $true } } # Copy container if (! $p2fError) { foreach ($container in $containers) { $target = $root + "\" + $container $src = $source + "\" + $container Try { Remove-Item -Recurse -ErrorAction stop $target Copy-Item -Recurse -ErrorAction stop $src $target } Catch [system.exception] { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Error on container " + $container } } } # start services [array]::Reverse($stopservices) foreach($service in $stopservices) { Try { $a = Get-Service -ErrorAction Stop -Name $service if($a.Status -ne "running") { Start-Service -ErrorAction Stop -InputObject $a } } Catch { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Error on starting service " + $service } } # remove source root If (! $p2fError) { Try { Remove-Item -Recurse -ErrorAction stop $source } Catch { Write-EventLog -LogName Application -Source p2fServer -Category 0 -EntryType error -EventId 5000 -Message "Error on removing " + $source } } exit 0