Add custom path option to UserProfileBackup

This commit is contained in:
Zachary Gorman 2024-06-11 11:59:33 -07:00
parent 4a01da0e5d
commit 157cc390f0

View file

@ -1,4 +1,4 @@
function Get-UserProfileBackup($Computer) {  function Get-UserProfileBackup ($Computer) {
<# <#
.SYNOPSIS .SYNOPSIS
Backs up a user profile. Backs up a user profile.
@ -10,21 +10,7 @@
#Charles Beddow maintains the UserProfileBackup #Charles Beddow maintains the UserProfileBackup
#The folders that this back up file grabs if($null -eq $Computer){
$FoldersToCopy = @(
'Desktop'
'Downloads'
'Favorites'
'Documents'
'Pictures'
'Videos'
'AppData\Local\Google\Chrome\User Data\Default\Bookmarks'
'AppData\Roaming\Microsoft'
'Sticky Notes'
)
if($Computer -eq $null){
$Computer = Read-Host -Prompt 'Enter computer name: ' $Computer = Read-Host -Prompt 'Enter computer name: '
} }
$ConfirmUser = $null $ConfirmUser = $null
@ -32,17 +18,19 @@
#Collects the most recent users for options to backup #Collects the most recent users for options to backup
if($Computer -eq $env:COMPUTERNAME) if($Computer -eq $env:COMPUTERNAME)
{ {
Import-Module "\\basagh\team\shsisdesktopsolutions\Powershell\Get-PC\Modules\PCLocal\PCLocal.psm1" $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
#Below are commands for the local PC when using "get-pc" by itself if($null -eq $lastUser){
$lastUser = Get-PCLastUserLocal Write-warning "PC unreachable or no profiles to back up"
return
}
} }
else { else {
$lastUser = Invoke-Command -ComputerName $Computer -SessionOption (New-PSSessionOption -NoMachineProfile) -ScriptBlock { $lastUser = Invoke-Command -ComputerName $Computer -SessionOption (New-PSSessionOption -NoMachineProfile) -ScriptBlock {
Get-ChildItem -Path C:\Users -Directory -Force -Exclude Public,Default,'Default User','All Users' | 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 } Sort-Object -Property LastWriteTime -Descending | Select-Object -First 3 -ExpandProperty Name }
if($lastUser -eq $null){ if($null -eq $lastUser){
Write-warning "PC unreachable or no profiles to back up" Write-warning "PC unreachable or no profiles to back up"
return return
} }
@ -51,7 +39,7 @@
Write-Host "Latest Users: $lastUser" Write-Host "Latest Users: $lastUser"
#Asks user to pick which profile to backup #Asks user to pick which profile to backup
while( $ConfirmUser -eq $null ){ while($null -eq $ConfirmUser){
$User = Read-Host -Prompt 'Choose user to backup' $User = Read-Host -Prompt 'Choose user to backup'
if( -not ( Test-Path -Path "\\$Computer\c$\Users\$User" -PathType Container ) ){ if( -not ( Test-Path -Path "\\$Computer\c$\Users\$User" -PathType Container ) ){
@ -66,13 +54,6 @@
break break
} }
} }
<#
Write-Host "Scanning user profile..."
$profileSize = scan-dir "\\$Computer\c$\Users\$User"
$sizeout = "{1} profile is {0:n3}GB" -f $profileSize,$user
Write-Host $sizeout
#>
$ConfirmUser = Read-Host -Prompt "Copy profile? (y/n) or Restore profile (r)" $ConfirmUser = Read-Host -Prompt "Copy profile? (y/n) or Restore profile (r)"
@ -144,77 +125,59 @@
#Chooses Site to Backup the profile to #Chooses Site to Backup the profile to
Write-Host "Choose Site to backup" 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 $out = "[{0}]: {1}" -f $i, $backupLocations[$i].Site
Write-Host $out Write-Host $out
} }
Write-Host "[$i]: Custom destination"
[int]$site = Read-Host "Site" [int]$site = Read-Host "Site"
if(!(($site -ge 0 ) -and ($site -lt 8))){ if(($site -ge 0 ) -and ($site -lt $numSites)){
Write-Warning "Invalid input please restart script"
return
}
$BackupDestination = $backupLocations[$site].Path $BackupDestination = $backupLocations[$site].Path
$Destination = "$BackupDestination\$DestinationFileName" $Destination = "$BackupDestination\$DestinationFileName"
#Creates a backup directory named "ComputerName-User-TodaysDate" #Creates a backup directory named "ComputerName-User-TodaysDate"
New-Item -Path $Destination -ItemType Directory | Write-Verbose New-Item -Path $Destination -ItemType Directory | Write-Verbose
if($User -eq "Public"){ } elseif ($site -eq $numSites) {
$FoldersToCopy = @('Desktop') $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 #Creates a log file
New-Item -Path $Destination\backuplog.txt -Force | Write-Verbose New-Item -Path $Destination\backuplog.txt -Force | Write-Verbose
$log = Join-Path -Path $Destination -ChildPath backuplog.txt $log = Join-Path -Path $Destination -ChildPath backuplog.txt
Write-Host "Copying profile to $Destination" Invoke-UserProfileTransfer $SourceRoot $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"
}
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 #Cleans up Citrix Shortcuts
Write-Host "Removing Citrix Shortcuts from backup" Write-Host "Removing Citrix Shortcuts from backup"
$Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk $Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk
foreach($Shortcut in $Shortcuts){ foreach($Shortcut in $Shortcuts){
$sh = New-Object -ComObject WScript.Shell $sh = New-Object -ComObject WScript.Shell
@ -222,8 +185,8 @@
Remove-Item $Shortcut.FullName Remove-Item $Shortcut.FullName
} }
} }
}
Write-Host "Complete" Write-Host "Complete"
} }
#endregion #endregion
@ -279,68 +242,6 @@
Write-Warning "The backup you have chosen does not exist" Write-Warning "The backup you have chosen does not exist"
break break
} }
if($User -eq "Public"){ UserProfileTransfer $FullRestorationSource $SourceRoot
$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"
} }
} }
#endregion
}#End Function
#Helper Function
function scan-dir($Path) {
if ( (Test-Path $Path) -and (Get-Item $Path).PSIsContainer ){
$DirectorySize = (Get-ChildItem -Path $path -File -Recurse -Force -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum / 1GB
Return $DirectorySize
}
}