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:
commit
c6457d4684
|
|
@ -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,20 +10,6 @@
|
||||||
|
|
||||||
#Charles Beddow maintains the UserProfileBackup
|
#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){
|
if($null -eq $Computer){
|
||||||
$Computer = Read-Host -Prompt 'Enter computer name: '
|
$Computer = Read-Host -Prompt 'Enter computer name: '
|
||||||
}
|
}
|
||||||
|
|
@ -31,11 +17,13 @@
|
||||||
|
|
||||||
#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 {
|
||||||
|
|
||||||
|
|
@ -118,11 +106,6 @@
|
||||||
Site='SPCH'
|
Site='SPCH'
|
||||||
Path="\\SHSCMSPCH\ProfileBackup"}
|
Path="\\SHSCMSPCH\ProfileBackup"}
|
||||||
$backupLocations += $obj
|
$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"
|
$Date = Get-Date -Format "MM-dd"
|
||||||
$DestinationFileName = "$Computer-$User-$Date"
|
$DestinationFileName = "$Computer-$User-$Date"
|
||||||
|
|
@ -137,86 +120,68 @@
|
||||||
#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)){
|
||||||
|
$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"
|
Write-Warning "Invalid input please restart script"
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
$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')
|
|
||||||
}
|
|
||||||
#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"
|
||||||
|
$Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk
|
||||||
|
|
||||||
Write-Host "Removing Citrix Shortcuts from backup"
|
foreach($Shortcut in $Shortcuts){
|
||||||
$Shortcuts = Get-ChildItem -Path $CopiedUserDesktop -Filter *.lnk
|
$sh = New-Object -ComObject WScript.Shell
|
||||||
|
|
||||||
|
if($sh.CreateShortcut($Shortcut.FullName).TargetPath -eq 'C:\Program Files (x86)\Citrix\ICA Client\SelfServicePlugin\SelfService.exe') {
|
||||||
foreach($Shortcut in $Shortcuts){
|
Remove-Item $Shortcut.FullName
|
||||||
$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"
|
Write-Host "Complete"
|
||||||
|
|
||||||
}
|
}
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
@ -272,57 +237,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
|
|
||||||
|
|
|
||||||
89
Private/UserProfileTransfer.ps1
Normal file
89
Private/UserProfileTransfer.ps1
Normal 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
|
||||||
Loading…
Reference in a new issue