I hooked up a quick script to backup important items from a PC. It’s is a quick and dirty solution that I may revisit sometime in the future with enhancements.
1: # http://geekswithblogs.net/Lance/archive/2009/12/29/program-files-environment-variable-in-powershell.aspx2: # http://itknowledgeexchange.techtarget.com/powershell/deleting-files-older-than-a-certain-date/3:4: $Today = (Get-Date -format "yyyy-MM-dd")5: $targetFolder = "\\BackupServer\Share"6: $backupDir = "$targetFolder\$env:username\$Today"7:8: function Remove-Items9: {10: $testFolderPath = $targetFolder11:12: if (Test-Path -Path $testFolderPath)13: {14: $fiveDaysAgo = (Get-Date).AddDays(-5)15: Get-ChildItem -Path $testFolderPath -Recurse | Where-Object { $_.CreationTime -lt $fiveDaysAgo } | Remove-Item -recurse -force16: }17: else18: {19: Write-Host "$testFolderPath does not exist."20: #Kill Script21: Exit22: }23: }24:25: function is64bit()26: {27: return ([IntPtr]::Size -eq 8)28: }29:30: function Get-ProgramFilesDir()31: {32: if (is64bit -eq $true) {33: (Get-Item "Env:ProgramFiles(x86)").Value34: }35: else {36: (Get-Item "Env:ProgramFiles").Value37: }38: }39:40: function Create-Folders41: {42: if (Test-Path -Path $backupDir)43: {44: #do nothing. Folders have been setup already45: }46: else47: {48: md "$backupDir" -force49: }50: }51:52: # Remove Folders older than $fivedaysago53: Remove-Items54:55: # Kills Outlook in preparation for backup56: if((get-process "OUTLOOK" -ea SilentlyContinue) -eq $Null){ }else{ Stop-Process -processname Outlook }57:58: # Start backup operation59: Create-Folders60:61: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:appdata\Microsoft\Outlook" "$backupDir\OutlookAppdata" /LOG+:"$backupDir\backup.log"62: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\AppData\Local\Microsoft\Outlook" "$backupDir\Outlook" /LOG+:"$backupDir\backup.log"63: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\Documents" "$backupDir\Docs" /LOG+:"$backupDir\backup.log"64: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\Favorites" "$backupDir\Favorites" /LOG+:"$backupDir\backup.log"65: #Robocopy /MIR /Z /COPYALL /MT:20 /R:5 /W:2 /NP "$env:userprofile\Desktop" "$backupDir\Desktop" /LOG+:"$backupDir\backup.log"66:67: #Start Outlook68: $programfilespath = Get-ProgramFilesDir69:70: if (Test-Path -Path "$programfilespath\Microsoft Office\Office12\Outlook.exe")71: {72: Start-Process "$programfilespath\Microsoft Office\Office12\Outlook.exe"73: }74: else75: {76: Start-Process "$programfilespath\Microsoft Office\Office11\Outlook.exe"77: }
Please note: This script is designed for Vista and above. It uses Robocopy to backup. You can get Robocopy on XP by installing the Resource Kit Tools. I’ve only tested this on Vista and above though. I’m also using version 2 of PowerShell.
No comments:
Post a Comment