Run Backup/Restore script for current logged in user

I am using a backup and restore script I found on github, that backs up user profiles to a network share, but I’m running into an issue. When I tested it with my other IT folks, they are all Admins, so there wasn’t an issue, but when I tested it on a non-Admin user, there was an issue. When I run the backup script, it’s supposed to look for the currently logged in user to the system, not PS. I’m using $env:username for the current user, but it must think I’m logged in, even though I only logged into PS prompt as Admin.

I just need to figure out how to force it to backup the system user, not the PS prompt user. I hope that makes sense.

Brian

I’m using $env:username

Not having access to your code, I am going to assume that your “PS Prompt” is a result of an Enter-PSSession command? If that is the case, then $ENV:UserName will return the username of that Runspace which will be you. You might try something like this to get the current logged in user?? Again, I am making assumptions …

(Get-CimInstance -ClassName Win32_LoggedOnUser).Antecedent.Name

I’m actually not using Enter-PSSession, as I run the script from the source computer. Let me post my code just in case you have a different idea. This is not my script; I just modified it to remove things I don’t need to backup/restore.

##region Switch to Backup
Start-Sleep -Seconds 5
Write-Host “”
Write-Host “”
Write-Host “”
Write-Host -ForegroundColor Yellow “Standby Loading Backup Script…”
Start-Sleep -Seconds 5
Clear-Host
##end region Switch to Backup

#region DeclaringBackuplocation
$destination = Join-Path -Path “\server\share” -ChildPath $env:USERNAME

#endregion DeclaringBackuplocation

#region DeclaringDataBackupSources
$folder = “Desktop”,
#“Downloads”,
#“Favorites”,
“Documents”,
“Pictures”,
“Videos”,
“AppData\Roaming\Microsoft\Signatures”
#“AppData\Local\Google\Chrome\User Data\Default”

#endregion DeclaringDataBackupSources

#Backup Chrome bookmarks
#$chromeBookmarksPath = Join-Path -Path $env:USERPROFILE -ChildPath #“\AppData\Local\Google\Chrome\User Data\Default\Bookmarks.bak”
#$chromeBookmarks = Get-ChildItem $chromeBookmarksPath
#$chromeBookmarks

#$chromeBookmarks_DestinationPath = Join-Path $destination -ChildPath #"\AppData\Local\Google\Chrome\User Data\Default"

#“ChromeBookmarks_DestinationPath: $chromeBookmarks_DestinationPath”

#Copy-Item -Path $chromeBookmarks_DestinationPath -Destination $chromeBookmarks.BaseName

#Robocopy.exe /mir $chromeBookmarksPath $chromeBookmarks_DestinationPath

Backup Data section

Write-Host “”
Write-Host “”
Write-Host “”
Write-Host -ForegroundColor green “Backing up data from local machine for $env:USERNAME”

#region DataBackup
ForEach ($f in $folder) {
$currentLocalFolder = Join-Path -Path $env:USERPROFILE -ChildPath $f

$currentRemoteFolder = Join-Path -Path $destination -ChildPath $f

$currentFolderSize = (Get-ChildItem -ErrorAction silentlyContinue $currentLocalFolder -Recurse -Force | Measure-Object -ErrorAction silentlyContinue -Property Length -Sum ).Sum / 1MB
$currentFolderSizeRounded = [System.Math]::Round($currentFolderSize)
Write-Host -ForegroundColor cyan "  $f... ($currentFolderSizeRounded MB)"

Copy-Item -Force -ErrorAction silentlyContinue -recurse $currentLocalFolder $currentRemoteFolder

}

#endregion DataBackup

Write-Host “”
Write-Host -ForegroundColor green “Registry Backup Complete”
#>
Write-Host “”
Write-Host “”
Write-Host -ForegroundColor green “Backup complete!”

would typing the in the username be acceptable? maybe something like:

$user = read-host "Enter a username"

or do you want it totally unattended?

Good question. What is your use case? Also, you dont give any details on how it is failing for non admin users. Please provide those details.

I want it unattended.

It’s not failing; it grabbed my profile, but it’s an empty one. I found what I needed:

$currentUser = gwmi Win32_ComputerSystem | select UserName

$currentUser = $currentUser.UserName.Split("")[1]

$destination = Join-Path -Path \veeam2\usmt -ChildPath $currentUser

Glad you got it figured and thanks for sharing for others to benefit.