Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ######################################################################################
- ## ##
- ## Change the vpn address to match the site ##
- ## ##
- ######################################################################################
- ############# Define what is a valid IP address to synchronize over
- $vpn_ip = 'x.x.x.*'
- $site_ip = 'x.x.x.*'
- $IPs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | select IPAddress
- $IPs = echo ($IPs.IPAddress | where{$_ -notmatch ":"})
- $validIP = $false
- $vpnIP = $false
- foreach($ip in $IPs){
- if($ip -like $site_ip){
- $validIP = $true
- if($ip -like $vpn_ip){
- $vpnIP = $true
- }
- }
- }
- ######################################################################################
- ## ##
- ## Change the local and network paths to match the site ##
- ## ##
- ######################################################################################
- ############# Run this script only of the user is connected to a local network
- if(($validIP) -and !($vpnIP)){
- ############# Define the local and network root directories for synchronization
- $local_path = "$env:USERPROFILE"
- $network_path = "\\server\share\$env:USERNAME"
- ############# Make sure all synchronize location have a cache root directory
- if(!(Test-Path $local_path\.bkup_cache)){
- $(MkDir $local_path\.bkup_cache).Attributes = βHiddenβ
- }
- if(!(Test-Path $local_path\.bkup_cache\Contacts)){
- MkDir $local_path\.bkup_cache\Contacts
- }
- if(!(Test-Path $local_path\.bkup_cache\Desktop)){
- MkDir $local_path\.bkup_cache\Desktop
- }
- if(!(Test-Path $local_path\.bkup_cache\Documents)){
- MkDir $local_path\.bkup_cache\Documents
- }
- if(!(Test-Path $local_path\.bkup_cache\Favorites)){
- MkDir $local_path\.bkup_cache\Favorites
- }
- if(!(Test-Path $local_path\.bkup_cache\Links)){
- MkDir $local_path\.bkup_cache\Links
- }
- ############# Generate a list of files that need to be checked for deletion
- $contacts = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Contacts)
- $desktop = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Desktop)
- $documents = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Documents)
- $favorites = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Favorites)
- $links = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Links)
- ############# The folder and file lists that will be synchronized
- $sync_folders = @($contacts,$desktop,$documents,$favorites,$links)
- $folder_names = @("Contacts","Desktop","Documents","Favorites","Links")
- #############
- # If a for each file that exists in the local cache, check to see if it exists
- # on the local machine, if it exists locally but not remotely, delete the local
- # copy as it once exists but now does not - meaning deleted from another location.
- # Check to see if the file exists in the cache, and on the network, but not locally
- # then delete the network copy becuase the file was deleted locally
- #############
- function Test-Deleted ($check_folder, $check_file){
- if (Test-Path "$local_path\$check_folder\$check_file") {
- if(!(Test-Path "$network_path\$check_folder\$check_file")){
- Remove-Item "$local_path\$check_folder\$check_file"
- }
- }
- if (Test-Path "$network_path\$check_folder\$check_file") {
- if (!(Test-Path "$local_path\$check_folder\$check_file")) {
- Remove-Item "$network_path\$check_folder\$check_file"
- }
- }
- }
- ############# Test the file locations to determine if a file was deleted and should be removed from a location
- $i = 0
- foreach ($folder in $folder_names) {
- foreach ($file in $sync_folders[$i]) {
- Test-Deleted $folder $file
- }
- $i++
- }
- ############# Update the network store location with any local file changes
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Contacts $network_path\Contacts
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Desktop $network_path\Desktop
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Documents $network_path\Documents
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Favorites $network_path\Favorites
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Links $network_path\Links
- ############# Download any updates to files from the network store location
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Contacts $local_path\Contacts
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Desktop $local_path\Desktop
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Documents $local_path\Documents
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Favorites $local_path\Favorites
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Links $local_path\Links
- ############# Update the local side cache with newest versions for Test-Delete function to compare on next run
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Contacts $local_path\.bkup_cache\Contacts
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Desktop $local_path\.bkup_cache\Desktop
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Documents $local_path\.bkup_cache\Documents
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Favorites $local_path\.bkup_cache\Favorites
- robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Links $local_path\.bkup_cache\Links
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement