riahc3

Untitled

Aug 21st, 2015
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $username = ([adsi]"WinNT://$env:userdomain/$env:username,user").fullname
  2. $userfolder = Join-Path '\\cd2012\EMAILBACKUP\' ($username.ToString())
  3. $pcfolder = Join-Path $userfolder $env:COMPUTERNAME
  4.  
  5.  if ($env:username -ne "excludeduser")
  6.  {
  7.         $wshell = New-Object -ComObject Wscript.Shell -ErrorAction SilentlyContinue
  8.         $wshell.Popup("Starting backups of emails",64)
  9.         Stop-Process -ProcessName Thunderbird* -Force -ErrorAction 'silentlycontinue'
  10.         Stop-Process -ProcessName Outlook* -Force -ErrorAction 'silentlycontinue'
  11.  
  12.  
  13. if ((Test-Path $userfolder) -and (Test-Path $pcfolder))
  14.     {
  15.             Write-Host "Last write"
  16.             $wts = (get-item $pcfolder).LastWriteTime
  17.             $timespan = new-timespan -days 2
  18.  
  19.  
  20.  
  21.            
  22.             if (((get-date) - $wts) -gt $timespan)
  23.                 {
  24.  
  25.                   $res=Test-Path "$env:LOCALAPPDATA\Microsoft\Outlook\"
  26.                     if ($res -eq $True)
  27.                         {
  28.                             & c:\windows\system32\Robocopy.exe "$env:LOCALAPPDATA\Microsoft\Outlook\" "$pcfolder" /E /MT:20 /ZB /R:3 /W:5 /V /ETA /LOG:\\cd2012\EMAILBACKUP\backuplog$username$(Get-Date -format 'yyyMMdd-HHmmssfff').txt
  29.                    
  30.                         }
  31.                         $res=Test-Path "$env:APPDATA\Thunderbird\Profiles\"
  32.                     if ($res -eq $True)
  33.                         {
  34.                             & c:\windows\system32\Robocopy.exe "$env:APPDATA\Thunderbird\Profiles\" "$pcfolder" /E /MT:20 /ZB /R:3 /W:5 /V /ETA /LOG:\\cd2012\EMAILBACKUP\backuplog$username$(Get-Date -format 'yyyMMdd-HHmmssfff').txt
  35.                  
  36.                         }
  37.              
  38.                     Set-ItemProperty -Path $userfolder -Name LastWriteTime -Value $(Get-Date)  
  39.                     Set-ItemProperty -Path $pcfolder -Name LastWriteTime -Value $(Get-Date)  
  40.    
  41.          
  42.                 }
  43.     }
  44.     else
  45.     {
  46.  
  47.  
  48.         if (-not (Test-Path $userfolder))
  49.         {
  50.            
  51.             New-Item -ItemType directory -Path $userfolder
  52.             $acl = Get-Acl $userfolder
  53.             $acl.Access | %{$acl.RemoveAccessRule($_)}
  54.             $acl.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME)
  55.             $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($env:USERNAME,'FullControl','Allow')
  56.             $acl.AddAccessRule($rule)
  57.             $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('myusernamehere','FullControl','Allow')
  58.             $acl.AddAccessRule($rule)
  59.             Set-Acl $userfolder $acl | Out-Null
  60.             $acl = Get-Acl $userfolder
  61.             $acl.SetAccessRuleProtection($True, $False)
  62.             Set-Acl $userfolder $acl | Out-Null
  63.          }
  64.  
  65.          if (-not (Test-Path $pcfolder))
  66.          {
  67.                     New-Item -ItemType directory -Path $pcfolder
  68.                     $acl2 = Get-Acl $pcfolder
  69.                     $acl2.Access | %{$acl2.RemoveAccessRule($_)}
  70.                     $acl2.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME)
  71.                     $rule2 = New-Object System.Security.AccessControl.FileSystemAccessRule($env:USERNAME,'FullControl','Allow')
  72.                     $acl2.AddAccessRule($rule2)
  73.                     $rule2 = New-Object System.Security.AccessControl.FileSystemAccessRule('myusernamehere','FullControl','Allow')
  74.                     $acl2.AddAccessRule($rule2)
  75.                     Set-Acl $pcfolder $acl2 | Out-Null  
  76.                     $acl2 = Get-Acl $pcfolder
  77.                     $acl2.SetAccessRuleProtection($False,$True)  
  78.                     Set-Acl $pcfolder $acl2 | Out-Null  
  79.          }
  80.  
  81.  
  82.  
  83.             $res=Test-Path "$env:LOCALAPPDATA\Microsoft\Outlook\"
  84.             if ($res -eq $True)
  85.             {
  86.                 & c:\windows\system32\Robocopy.exe "$env:LOCALAPPDATA\Microsoft\Outlook\" "$pcfolder" /E /MT:20 /ZB /R:3 /W:5 /V /ETA /LOG:\\cd2012\EMAILBACKUP\backuplog$username$(Get-Date -format 'yyyMMdd-HHmmssfff').txt
  87.                }
  88.    
  89.             $res=Test-Path "$env:APPDATA\Thunderbird\Profiles\"
  90.             if ($res -eq $True)
  91.             {
  92.  
  93.                 & c:\windows\system32\Robocopy.exe "$env:APPDATA\Thunderbird\Profiles\" "$pcfolder" /E /MT:20 /ZB /R:3 /W:5 /V /ETA /LOG:\\cd2012\EMAILBACKUP\backuplog$username$(Get-Date -format 'yyyMMdd-HHmmssfff').txt
  94.                }
  95.              Set-ItemProperty -Path $userfolder -Name LastWriteTime -Value $(Get-Date)  
  96.              Set-ItemProperty -Path $pcfolder -Name LastWriteTime -Value $(Get-Date)
  97.         }
  98.  
  99.    
  100.  
  101.  
  102. }
  103.  
  104. Write-Host "Finished"
Advertisement
Add Comment
Please, Sign In to add comment