Advertisement
Guest User

Folder Redirection Alternative

a guest
Jun 11th, 2014
397
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ######################################################################################
  2. ##                                          ##
  3. ##          Change the vpn address to match the site            ##
  4. ##                                          ##
  5. ######################################################################################
  6.  
  7. ############# Define what is a valid IP address to synchronize over
  8.  
  9. $vpn_ip = 'x.x.x.*'
  10. $site_ip = 'x.x.x.*'
  11.  
  12. $IPs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName . | select IPAddress
  13. $IPs = echo ($IPs.IPAddress | where{$_ -notmatch ":"})
  14. $validIP = $false
  15. $vpnIP = $false
  16. foreach($ip in $IPs){
  17.     if($ip -like $site_ip){
  18.         $validIP = $true
  19.         if($ip -like $vpn_ip){
  20.             $vpnIP = $true
  21.         }
  22.     }
  23. }
  24.  
  25. ######################################################################################
  26. ##                                          ##
  27. ##      Change the local and network paths to match the site            ##
  28. ##                                          ##
  29. ######################################################################################
  30.  
  31. ############# Run this script only of the user is connected to a local network
  32.  
  33. if(($validIP) -and !($vpnIP)){
  34.  
  35.     ############# Define the local and network root directories for synchronization
  36.  
  37.     $local_path = "$env:USERPROFILE"
  38.     $network_path = "\\server\share\$env:USERNAME"
  39.  
  40.     ############# Make sure all synchronize location have a cache root directory
  41.  
  42.     if(!(Test-Path $local_path\.bkup_cache)){
  43.         $(MkDir $local_path\.bkup_cache).Attributes = β€˜Hidden’
  44.     }
  45.     if(!(Test-Path $local_path\.bkup_cache\Contacts)){
  46.         MkDir $local_path\.bkup_cache\Contacts
  47.     }
  48.     if(!(Test-Path $local_path\.bkup_cache\Desktop)){
  49.         MkDir $local_path\.bkup_cache\Desktop
  50.     }
  51.     if(!(Test-Path $local_path\.bkup_cache\Documents)){
  52.         MkDir $local_path\.bkup_cache\Documents
  53.     }
  54.     if(!(Test-Path $local_path\.bkup_cache\Favorites)){
  55.         MkDir $local_path\.bkup_cache\Favorites
  56.     }
  57.     if(!(Test-Path $local_path\.bkup_cache\Links)){
  58.         MkDir $local_path\.bkup_cache\Links
  59.     }
  60.  
  61.     ############# Generate a list of files that need to be checked for deletion
  62.  
  63.     $contacts = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Contacts)
  64.     $desktop = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Desktop)
  65.     $documents = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Documents)
  66.     $favorites = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Favorites)
  67.     $links = @(Get-ChildItem -Recurse -Name $local_path\.bkup_cache\Links)
  68.  
  69.     ############# The folder and file lists that will be synchronized
  70.  
  71.     $sync_folders = @($contacts,$desktop,$documents,$favorites,$links)
  72.     $folder_names = @("Contacts","Desktop","Documents","Favorites","Links")
  73.    
  74.     #############
  75.     # If a for each file that exists in the local cache, check to see if it exists
  76.     # on the local machine, if it exists locally but not remotely, delete the local
  77.     # copy as it once exists but now does not - meaning deleted from another location.
  78.     # Check to see if the file exists in the cache, and on the network, but not locally
  79.     # then delete the network copy becuase the file was deleted locally
  80.     #############
  81.  
  82.     function Test-Deleted ($check_folder, $check_file){
  83.         if (Test-Path "$local_path\$check_folder\$check_file") {
  84.             if(!(Test-Path "$network_path\$check_folder\$check_file")){
  85.                 Remove-Item "$local_path\$check_folder\$check_file"
  86.             }
  87.         }
  88.         if (Test-Path "$network_path\$check_folder\$check_file") {
  89.             if (!(Test-Path "$local_path\$check_folder\$check_file")) {
  90.                 Remove-Item "$network_path\$check_folder\$check_file"
  91.             }
  92.         }
  93.  
  94.     }
  95.  
  96.     ############# Test the file locations to determine if a file was deleted and should be removed from a location
  97.  
  98.     $i = 0
  99.     foreach ($folder in $folder_names) {
  100.         foreach ($file in $sync_folders[$i]) {
  101.             Test-Deleted $folder $file
  102.         }
  103.         $i++
  104.     }
  105.  
  106.     ############# Update the network store location with any local file changes
  107.    
  108.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Contacts $network_path\Contacts
  109.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Desktop $network_path\Desktop
  110.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Documents $network_path\Documents
  111.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Favorites $network_path\Favorites
  112.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $local_path\Links $network_path\Links
  113.    
  114.     ############# Download any updates to files from the network store location
  115.  
  116.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Contacts $local_path\Contacts
  117.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Desktop $local_path\Desktop
  118.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Documents $local_path\Documents
  119.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Favorites $local_path\Favorites
  120.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /XO /E /SEC /DCOPY:DAT /XJ /R:0 /IPG:2 $network_path\Links $local_path\Links
  121.  
  122.     ############# Update the local side cache with newest versions for Test-Delete function to compare on next run
  123.    
  124.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Contacts $local_path\.bkup_cache\Contacts
  125.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Desktop $local_path\.bkup_cache\Desktop
  126.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Documents $local_path\.bkup_cache\Documents
  127.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Favorites $local_path\.bkup_cache\Favorites
  128.     robocopy /NFL /NDL /NJH /NJS /NC /NS /NP /MIR /SEC /DCOPY:DAT /XJ /R:0 $local_path\Links $local_path\.bkup_cache\Links
  129.  
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement