Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $username = ([adsi]"WinNT://$env:userdomain/$env:username,user").fullname
- $userfolder = Join-Path '\\cd2012\EMAILBACKUP\' ($username.ToString())
- $pcfolder = Join-Path $userfolder $env:COMPUTERNAME
- if ($env:username -ne "excludeduser")
- {
- $wshell = New-Object -ComObject Wscript.Shell -ErrorAction SilentlyContinue
- $wshell.Popup("Starting backups of emails",64)
- Stop-Process -ProcessName Thunderbird* -Force -ErrorAction 'silentlycontinue'
- Stop-Process -ProcessName Outlook* -Force -ErrorAction 'silentlycontinue'
- if ((Test-Path $userfolder) -and (Test-Path $pcfolder))
- {
- Write-Host "Last write"
- $wts = (get-item $pcfolder).LastWriteTime
- $timespan = new-timespan -days 2
- if (((get-date) - $wts) -gt $timespan)
- {
- $res=Test-Path "$env:LOCALAPPDATA\Microsoft\Outlook\"
- if ($res -eq $True)
- {
- & 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
- }
- $res=Test-Path "$env:APPDATA\Thunderbird\Profiles\"
- if ($res -eq $True)
- {
- & 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
- }
- Set-ItemProperty -Path $userfolder -Name LastWriteTime -Value $(Get-Date)
- Set-ItemProperty -Path $pcfolder -Name LastWriteTime -Value $(Get-Date)
- }
- }
- else
- {
- if (-not (Test-Path $userfolder))
- {
- New-Item -ItemType directory -Path $userfolder
- $acl = Get-Acl $userfolder
- $acl.Access | %{$acl.RemoveAccessRule($_)}
- $acl.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME)
- $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($env:USERNAME,'FullControl','Allow')
- $acl.AddAccessRule($rule)
- $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('myusernamehere','FullControl','Allow')
- $acl.AddAccessRule($rule)
- Set-Acl $userfolder $acl | Out-Null
- $acl = Get-Acl $userfolder
- $acl.SetAccessRuleProtection($True, $False)
- Set-Acl $userfolder $acl | Out-Null
- }
- if (-not (Test-Path $pcfolder))
- {
- New-Item -ItemType directory -Path $pcfolder
- $acl2 = Get-Acl $pcfolder
- $acl2.Access | %{$acl2.RemoveAccessRule($_)}
- $acl2.SetOwner([System.Security.Principal.NTAccount] $env:USERNAME)
- $rule2 = New-Object System.Security.AccessControl.FileSystemAccessRule($env:USERNAME,'FullControl','Allow')
- $acl2.AddAccessRule($rule2)
- $rule2 = New-Object System.Security.AccessControl.FileSystemAccessRule('myusernamehere','FullControl','Allow')
- $acl2.AddAccessRule($rule2)
- Set-Acl $pcfolder $acl2 | Out-Null
- $acl2 = Get-Acl $pcfolder
- $acl2.SetAccessRuleProtection($False,$True)
- Set-Acl $pcfolder $acl2 | Out-Null
- }
- $res=Test-Path "$env:LOCALAPPDATA\Microsoft\Outlook\"
- if ($res -eq $True)
- {
- & 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
- }
- $res=Test-Path "$env:APPDATA\Thunderbird\Profiles\"
- if ($res -eq $True)
- {
- & 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
- }
- Set-ItemProperty -Path $userfolder -Name LastWriteTime -Value $(Get-Date)
- Set-ItemProperty -Path $pcfolder -Name LastWriteTime -Value $(Get-Date)
- }
- }
- Write-Host "Finished"
Advertisement
Add Comment
Please, Sign In to add comment