Skip to content

Instantly share code, notes, and snippets.

@danjpadgett
Last active July 4, 2017 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danjpadgett/72474915f517df0d2489e4d3cdd8a9e1 to your computer and use it in GitHub Desktop.
Save danjpadgett/72474915f517df0d2489e4d3cdd8a9e1 to your computer and use it in GitHub Desktop.
Backup Windows Client Side Cache (CSC)
<#
.SYNOPSIS
Repermissions and backs up CSC Cache for logged in user
.DESCRIPTION
Script will locate running users csc cache and zip, then export to defined location. Requires 7 Zip.
.NOTES
Version: 1.0
Author: dpadgett
Creation Date: 07/02/17
Purpose/Change: Release
.EXAMPLE
Null
#>
Write-Host "Beginning Backup Task" -ForegroundColor Green
$domain = 'contoso.com'
$domainprefix = 'CORP'
$user = (Get-WmiObject –Class Win32_ComputerSystem | Select-Object UserName).Username.Trim("$domainprefix\")
$date = Get-Date -UFormat %d-%m-%y
$tempcopy = "C:\users\$user\appdata\local\temp"
$fileserver = "\\server\Profile_Backup$"
$logfile = "$fileserver\Log\$date-$user.txt"
$RobocopyParams = @("/r:1", "/IS", "/w:1", "/COPY:DAT", "/NJS", "/E", "/V", "/NP", "/TEE", "/ZB", "/COPYALL")
$XD = @("WINDOWS", "APPDATA", "SendTo", "Temp", "Temporary Internet Files", "Favorites", "`$RECYCLE.BIN")
$XF = @("*.pif", "*.pst", "*.EXE", "*.ini", "*.bat", "*.tmp", "thumbs.db")
$IC = @("Everyone:(F)", "/t", "/l", "/q")
$destination = "$fileserver\BackupRoot"
$tempcopy2 = "C:\users\$user\appdata\local\temp\$user"
$fullpath = "C:\Windows\CSC"
takeown /f $fullpath /r /a /d y
icacls $fullpath /grant @IC
$CSC = Get-ChildItem -path "C:\Windows\CSC\v2.0.6\namespace\$domain" -filter $user -Recurse | Select-Object -Expand FullName
#region CopyCSC
#Code should be run as elevated user but still read in the correct user for next steps
Write-Host "Taking Ownership of CSC Cache Locally" -ForegroundColor Green
foreach ($i in $CSC)
{
Write-Host "Copying CSC Cache to tempcopy" -ForegroundColor Green
robocopy.exe $i "$tempcopy\$user\CSC-Cache" $RobocopyParams /XD @XD /XF @XF /log+:$logfile
}
#endregion
#region ExtraFiles
Write-Host "Copying Downloads Dir to share" -ForegroundColor Green
$sourcepaths = "c:\users\$user\Downloads"
takeown /f $sourcepaths /r /a /d y
icacls $sourcepaths /grant @IC
foreach ($folder in $sourcepaths)
{
if ($folder -eq "c:\users\$user\Downloads") { $DLdestination = "$tempcopy\$user\downloads" }
Write-Host "Starting file copy from '$folder' to backup Root" -ForegroundColor Yellow
if (!(test-path $logfile -erroraction SilentlyContinue)) { New-Item $logfile -ItemType file }
robocopy.exe $folder $DLdestination $RobocopyParams /XD @XD /XF @XF /log+:$logfile
Write-Host "Copy Complete for $folder to $destination " -ForegroundColor Yellow
}
if (Test-Path "C:\users\$user\appdata\local\temp\$user.zip") {Remove-Item -Path "C:\users\$user\appdata\local\temp\$user.zip" -Force}
Write-Host "Compressing CSC..This will take some time" -ForegroundColor Green
#Install 7zip
if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {
$pathvargs = {.\7z1604.exe /S}
Invoke-Command -ScriptBlock $pathvargs
}
Do
{
Write-Host "Waiting on 7zip install..." -ForegroundColor Green
sleep 3
} until (test-path "$env:ProgramFiles\7-Zip\7z.exe"
)
set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"
sz a -t7z -mmt "$tempcopy2.7z" "$tempcopy2"
wait-process -Name 7z -ErrorAction SilentlyContinue
Write-Host "Permissioning Zip File" -ForegroundColor Green
icacls "$tempcopy\$user.7z" /grant @IC
Write-Host "Copying compressed CSC Cache to share" -ForegroundColor Green
robocopy.exe $tempcopy $destination "$user.7z"
Write-Host "All Done" -ForegroundColor Green
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment