Merge branch 'ProfileTransfer'

Merge adds a "Custom Path" option to -UserProfileBackup to enable
transferring directly to another PC. Invoke-UserProfileTransfer factored
out of Get-UserProfileBackup for simplicity.
This commit is contained in:
Zachary Gorman 2024-06-20 09:47:12 -07:00
commit c6457d4684
2 changed files with 143 additions and 140 deletions

View file

@ -10,20 +10,6 @@
#Charles Beddow maintains the UserProfileBackup
#The folders that this back up file grabs
$FoldersToCopy = @(
'Desktop'
'Downloads'
'Favorites'
'Documents'
'Pictures'
'Videos'
'AppData\Local\Google\Chrome\User Data\Default\Bookmarks'
'AppData\Roaming\Microsoft'
'Sticky Notes'
)
if($null -eq $Computer){
$Computer = Read-Host -Prompt 'Enter computer name: '
}
@ -32,10 +18,12 @@
#Collects the most recent users for options to backup
if($Computer -eq $env:COMPUTERNAME)
{
Import-Module "\\basagh\team\shsisdesktopsolutions\Powershell\Get-PC\Modules\PCLocal\PCLocal.psm1"
#Below are commands for the local PC when using "get-pc" by itself
$lastUser = Get-PCLastUserLocal
$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 {
@ -118,11 +106,6 @@
Site='SPCH'
Path="\\SHSCMSPCH\ProfileBackup"}
$backupLocations += $obj
$obj = New-Object PSObject -Property @{
Site='Old Team Share'
Path="\\basagh\team\SHSISDesktopSolutions\General Shared Items\User Profile Backup"}
$backupLocations += $obj
$Date = Get-Date -Format "MM-dd"
$DestinationFileName = "$Computer-$User-$Date"
@ -137,77 +120,59 @@
#Chooses Site to Backup the profile to
Write-Host "Choose Site to backup"
for($i=0;$i -lt 8; $i++){
$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 8))){
Write-Warning "Invalid input please restart script"
return
}
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
if($User -eq "Public"){
$FoldersToCopy = @('Desktop')
} 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
Write-Host "Copying profile to $Destination"
foreach( $Folder in $FoldersToCopy ){
$Source = Join-Path -Path $SourceRoot -ChildPath $Folder
Write-Host "Copying $Folder"
if( $Folder -eq 'Sticky Notes'){
$Source = Join-Path -Path $SourceRoot -ChildPath 'AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState'
}
#Checks if folder paths are available
if( -not ( Test-Path -Path $Source ) ){
Write-Warning "Could not find path`t$Source"
continue
}
if( -not ( Test-Path -Path $Destination -PathType Container ) ){
Write-Warning "Could not find path`t$Destination"
continue
}
if($Folder -eq 'AppData\Roaming\Microsoft'){
#If it's time to copy AppData folder it changes the copy destination to build the proper directory tree
$Destination = Join-Path -Path $Destination -ChildPath 'AppData\Roaming\Microsoft'
Copy-Item -Path (Get-Item -Path "$Source\*" -Exclude ('Teams')).FullName -Destination $Destination -Recurse -Force -PassThru | Out-File $log -Append
Write-Host 'complete'
continue
}
if ($Folder -eq 'Sticky Notes'){
$Destination = Join-Path -Path "$BackupDestination\$DestinationFileName" -ChildPath 'Sticky Notes'
}
Copy-Item $Source -Destination $Destination -Recurse -PassThru | Out-File $log -Append
Write-Host "complete"
}
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"
$Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk
foreach($Shortcut in $Shortcuts){
$sh = New-Object -ComObject WScript.Shell
@ -215,8 +180,8 @@
Remove-Item $Shortcut.FullName
}
}
}
Write-Host "Complete"
}
#endregion
@ -272,57 +237,6 @@
Write-Warning "The backup you have chosen does not exist"
break
}
if($User -eq "Public"){
$FoldersToCopy = @('Desktop')
}
foreach( $Folder in $FoldersToCopy ){
#Special Case for AppData
if($Folder -eq 'AppData\Roaming\Microsoft'){
$Folder = 'AppData'
}
$CurrentRestoreFolder = Join-Path -Path $FullRestorationSource -ChildPath $Folder
#Special Case for Google Bookmarks
if($Folder -eq 'AppData\Local\Google\Chrome\User Data\Default\Bookmarks'){
Write-Host "Restoring Chrome Bookmarks"
if( -not ( Test-Path -Path $FullRestorationSource\Bookmarks)){
Write-Host "No Chrome Bookmarks backup"
continue
}
if( -not ( Test-Path -Path "$SourceRoot\AppData\Local\Google\Chrome")){
Write-Warning "Restoring Chrome bookmarks but Chrome is not installed on this machine"
}
if( -not ( Test-Path -Path "$SourceRoot\AppData\Local\Google\Chrome\User Data\Default")){
New-Item "$SourceRoot\AppData\Local\Google\Chrome\User Data\Default" -ItemType Container | Write-Verbose
}
Copy-Item $FullRestorationSource\Bookmarks -Destination "$SourceRoot\AppData\Local\Google\Chrome\User Data\Default\Bookmarks" -Force
Write-Host "complete"
continue
}
#Special Case for Sticky Notes
if($Folder -eq 'Sticky Notes'){
continue
}
Write-Host "Restoring $Folder"
#Checks if folder paths are available
if( -not ( Test-Path -Path $CurrentRestoreFolder -PathType Container ) ){
Write-Warning "Could not find path`t$Source"
continue
}
if( -not ( Test-Path -Path $SourceRoot -PathType Container ) ){
Write-Warning "Could not find path`t$Destination"
continue
}
Copy-Item $CurrentRestoreFolder -Destination $SourceRoot -Recurse -Force
Write-Host "complete"
UserProfileTransfer $FullRestorationSource $SourceRoot
}
}
#endregion
}#End Function

View file

@ -0,0 +1,89 @@
function Invoke-UserProfileTransfer {
param($srcPath, $dstPath)
<#
.SYNOPSIS
Backs up a user profile to a specified filepath.
.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 to a specified filepath.
#>
#The folders that this back up file grabs
$FoldersToCopy = @(
'Desktop'
'Downloads'
'Favorites'
'Documents'
'Pictures'
'Videos'
'AppData\Local\Google\Chrome\User Data\Default\Bookmarks'
'AppData\Roaming\Microsoft'
'Sticky Notes'
)
#Creates a log file
New-Item -Path $dstPath\backuplog.txt -Force | Write-Verbose
$log = Join-Path -Path $dstPath -ChildPath backuplog.txt
Write-Host "Copying profile to $dstPath"
for ($i = 0; $i -lt $FoldersToCopy.Length; $i++) {
$Folder = $FoldersToCopy[$i]
Write-Progress -Activity "User Profile Transfer from $srcPath to $dstPath" -Status $Folder -PercentComplete ($i / $FoldersToCopy.Length)*100
$Source = Join-Path -Path $srcPath -ChildPath $Folder
$Destination = $dstPath
Write-Host "Copying $Folder"
if( $Folder -eq 'Sticky Notes'){
$Source = Join-Path -Path $srcPath -ChildPath 'AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState'
$Destination = Join-Path -Path "$dstPath" -ChildPath 'AppData\Local\Packages\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\LocalState'
New-Item -Path $Destination -ItemType Directory | Write-Verbose
}
if($Folder -eq 'AppData\Local\Google\Chrome\User Data\Default\Bookmarks'){
if( -not ( Test-Path -Path $Source )){
Write-Host "No Chrome Bookmarks present"
continue
}
if( -not ( Test-Path -Path "$Destination")){
Write-Warning "Restoring Chrome bookmarks but Chrome is not installed on this machine"
}
if( -not ( Test-Path -Path "$Destination")){
New-Item "$Destination" -ItemType Container | Write-Verbose
}
Copy-Item $Source -Destination "$Destination" -Force | Out-File $log -Append
Write-Host "complete"
continue
}
#Checks if folder paths are available
if( -not ( Test-Path -Path $Source ) ){
Write-Warning "Could not find path`t$Source"
continue
}
if( -not ( Test-Path -Path $Destination -PathType Container ) ){
Write-Warning "Could not find path`t$Destination"
continue
}
if($Folder -eq 'AppData\Roaming\Microsoft'){
#If it's time to copy AppData folder it changes the copy destination to build the proper directory tree
$Destination = Join-Path -Path $Destination -ChildPath 'AppData\Roaming\Microsoft'
Copy-Item -Path (Get-Item -Path "$Source\*" -Exclude ('Teams')).FullName -Destination $Destination -Recurse -Force -PassThru | Out-File $log -Append
Write-Host 'complete'
continue
}
Copy-Item $Source -Destination $Destination -Recurse -PassThru | Out-File $log -Append
Write-Host "complete"
}
Write-Progress -Activity "User Profile Transfer from $srcPath to $dstPath" -Completed
#endregion
}#End Function