Get-PC/Private/UserProfileBackup.ps1

244 lines
9.1 KiB
PowerShell

function Get-UserProfileBackup ($Computer) {
<#
.SYNOPSIS
Backs up a user profile.
.DESCRIPTION
Takes a input of hostname and user and will make a copy of the user profile
and the Microsoft Roaming Folder and back it up on the share drive.
#>
#Charles Beddow maintains the UserProfileBackup
if($null -eq $Computer){
$Computer = Read-Host -Prompt 'Enter computer name: '
}
$ConfirmUser = $null
#Collects the most recent users for options to backup
if($Computer -eq $env:COMPUTERNAME)
{
$lastUser = Get-ChildItem -Path C:\Users -Directory -Force -Exclude Public,Default,'Default User','All Users' |
Sort-Object -Property LastWriteTime -Descending | Select-Object -First 3 -ExpandProperty Name
if($null -eq $lastUser){
Write-warning "PC unreachable or no profiles to back up"
return
}
}
else {
$lastUser = Invoke-Command -ComputerName $Computer -SessionOption (New-PSSessionOption -NoMachineProfile) -ScriptBlock {
Get-ChildItem -Path C:\Users -Directory -Force -Exclude Public,Default,'Default User','All Users' |
Sort-Object -Property LastWriteTime -Descending | Select-Object -First 3 -ExpandProperty Name }
if($null -eq $lastUser){
Write-warning "PC unreachable or no profiles to back up"
return
}
}
Write-Host "Latest Users: $lastUser"
#Asks user to pick which profile to backup
while($null -eq $ConfirmUser){
$User = Read-Host -Prompt 'Choose user to backup'
if( -not ( Test-Path -Path "\\$Computer\c$\Users\$User" -PathType Container ) ){
Write-Warning "$User could not be found on $Computer. Please enter another user profile."
continue
}
else {
$testPath = Resolve-Path -Path "\\$Computer\c$\Users\$User\Desktop"
if (-not $testPath ){
Write-Warning "Unable to access profile"
Write-Warning "Please try running script as Admin"
break
}
}
$ConfirmUser = Read-Host -Prompt "Copy profile? (y/n) or Restore profile (r)"
if($ConfirmUser -eq 'n'){
$ConfirmUser = Read-Host -Prompt "Choose another user? (y/n)"
if($ConfirmUser -eq 'y'){
$ConfirmUser = $null
continue
}
}
}
if($ConfirmUser -eq 'n'){
Write-Host "User backup canceled"
}
#Backup Locations
$backupLocations = @()
$obj = New-Object PSObject -Property @{
Site='GSRMC'
Path="\\SHSCMGSRMC\ProfileBackup"}
$backupLocations += $obj
$obj = New-Object PSObject -Property @{
Site='SAGH'
Path="\\SHSCMSAGH\ProfileBackup"}
$backupLocations += $obj
$obj = New-Object PSObject -Property @{
Site='SNLH'
Path="\\SHSCMSNLH\ProfileBackup"}
$backupLocations += $obj
$obj= New-Object PSObject -Property @{
Site='LCH'
Path="\\shscmslch\ProfileBackup"}
$backupLocations += $obj
$obj = New-Object PSObject -Property @{
Site='Walnut'
Path="\\SHSCMWN\ProfileBackup"}
$backupLocations += $obj
$obj = New-Object PSObject -Property @{
Site='Sam Square'
Path="\\SHSCMAV\ProfileBackup"}
$backupLocations += $obj
$obj = New-Object PSObject -Property @{
Site='HP'
Path="\\SHSCM01\ProfileBackup"}
$backupLocations += $obj
$obj = New-Object PSObject -Property @{
Site='SPCH'
Path="\\SHSCMSPCH\ProfileBackup"}
$backupLocations += $obj
$Date = Get-Date -Format "MM-dd"
$DestinationFileName = "$Computer-$User-$Date"
$SourceRoot = "\\$Computer\c$\Users\$User"
$BackupDestination = "\\SHSCMGSRMC\ProfileBackup"
$Destination = "\\SHSCMGSRMC\ProfileBackup\$DestinationFileName"
#region BACKUP
if($ConfirmUser -eq 'y'){
#Chooses Site to Backup the profile to
Write-Host "Choose Site to backup"
$i = 0
$numSites = $backupLocations.length
for($i=0;$i -lt $numSites; $i++){
$out = "[{0}]: {1}" -f $i, $backupLocations[$i].Site
Write-Host $out
}
Write-Host "[$i]: Custom destination"
[int]$site = Read-Host "Site"
if(($site -ge 0 ) -and ($site -lt $numSites)){
$BackupDestination = $backupLocations[$site].Path
$Destination = "$BackupDestination\$DestinationFileName"
#Creates a backup directory named "ComputerName-User-TodaysDate"
New-Item -Path $Destination -ItemType Directory | Write-Verbose
} elseif ($site -eq $numSites) {
$BackupDestination = Read-Host "Enter a PC asset, hostname, or custom path"
# Parse input for asset tag, hostname, then full path
if ($BackupDestination -match "^[1-9]{5}$") {
$BackupDestination = Get-AssetConversion $BackupDestination
}
# Treat as hostname and see if it's reachable
if (Test-Connection -ComputerName $BackupDestination -Count 1) {
# If so, get the filesystem path
$BackupDestination = "\\$BackupDestination\c$\Users\$User"
}
# Finally, test if this path works
if (-not ($BackupDestination -and (Test-Path "$BackupDestination"))) {
Write-Warning "Unable to access [$BackupDestination]"
return
}
$Destination = $BackupDestination
} else {
Write-Warning "Invalid input please restart script"
return
}
#Creates a log file
New-Item -Path $Destination\backuplog.txt -Force | Write-Verbose
$log = Join-Path -Path $Destination -ChildPath backuplog.txt
Invoke-UserProfileTransfer $SourceRoot $Destination
if(($site -ge 0 ) -and ($site -lt $numSites)){
#Manually backup sticky notes since we don't always want them to transfer
$StickySource = Join-Path -Path $SourceRoot -ChildPath 'AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState'
$StickyDestination = Join-Path -Path "$Destination" -ChildPath 'AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState'
New-Item -Path $StickyDestination -ItemType Directory | Write-Verbose
Copy-Item $StickySource -Destination $StickyDestination -Recurse -PassThru | Out-File $log -Append
#Cleans up Citrix Shortcuts
Write-Host "Removing Citrix Shortcuts from backup"
$CopiedUserDesktop = Join-Path -Path $Destination -ChildPath "Desktop"
$Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk
foreach($Shortcut in $Shortcuts){
$sh = New-Object -ComObject WScript.Shell
if($sh.CreateShortcut($Shortcut.FullName).TargetPath -eq 'C:\Program Files (x86)\Citrix\ICA Client\SelfServicePlugin\SelfService.exe') {
Remove-Item $Shortcut.FullName
}
}
}
Write-Host "Complete"
}
#endregion
#region RESTORE
if($ConfirmUser -eq 'r'){
$backups = @()
foreach($b in $backupLocations) {
$paths = @()
$paths += Get-ChildItem -Path $b.Path -Name -Filter "*$User*"
foreach($p in $paths){
$obj = New-Object PSObject -Property @{
File = $p
Location = $b.Path
}
$backups += $obj
}
}
if($null -eq $backups[0]){
Write-Warning "no backups available for this user"
return
}
else{
Write-Host "These backups are available for this user:"
}
if($backups -is [array]) {
$i = 0
foreach ($backup in $backups){
$out = $backup.File
Write-Host "[$i] $out"
$i++
}
$FileToRestoreIndex = Read-Host -Prompt "Please type which directory you'd like to restore"
$FileToRestore = $backups[$FileToRestoreIndex].File
$BackupDestination = $backups[$FileToRestoreIndex].Location
}
else{
$out = $backups.File
Write-Host "[0] $out"
$FileToRestoreIndex = Read-Host -Prompt "Please confirm which directory you'd like to restore"
if($FileToRestoreIndex -eq 0){
$FileToRestore = $backups.File
$BackupDestination = $backups.Location
}
}
Write-Host "Restoring $FileToRestore"
Write-Host "--------------------"
$FullRestorationSource = Join-Path -Path $BackupDestination -ChildPath $FileToRestore
Write-Verbose $FullRestorationSource
if( -not ( Test-Path -Path $FullRestorationSource -PathType Container ) ){
Write-Warning "The backup you have chosen does not exist"
break
}
UserProfileTransfer $FullRestorationSource $SourceRoot
}
}