<# .SYNOPSIS Runs a backup script on user login .DESCRIPTION Backs up all key user data to Onedrive .INPUTS None required .OUTPUTS Within Azure .NOTES Version: 1.0 Author: Andrew Taylor Twitter: @AndrewTaylor_2 WWW: andrewstaylor.com Creation Date: 21/08/2021 Purpose/Change: Initial script development .EXAMPLE N/A #> Function Get-ScriptVersion(){ <# .SYNOPSIS This function is used to check if the running script is the latest version .DESCRIPTION This function checks GitHub and compares the 'live' version with the one running .EXAMPLE Get-ScriptVersion Returns a warning and URL if outdated .NOTES NAME: Get-ScriptVersion #> [cmdletbinding()] param ( $liveuri ) $contentheaderraw = (Invoke-WebRequest -Uri $liveuri -Method Get) $contentheader = $contentheaderraw.Content.Split([Environment]::NewLine) $liveversion = (($contentheader | Select-String 'Version:') -replace '[^0-9.]','') | Select-Object -First 1 $currentversion = ((Get-Content -Path $PSCommandPath | Select-String -Pattern "Version: *") -replace '[^0-9.]','') | Select-Object -First 1 if ($liveversion -ne $currentversion) { write-host "Script has been updated, please download the latest version from $liveuri" -ForegroundColor Red } } Get-ScriptVersion -liveuri "https://raw.githubusercontent.com/andrew-s-taylor/public/main/Powershell%20Scripts/Intune/backup-scheduled.ps1" #Create path for files $DirectoryToCreate = "c:\backup-restore" if (-not (Test-Path -LiteralPath $DirectoryToCreate)) { try { New-Item -Path $DirectoryToCreate -ItemType Directory -ErrorAction Stop | Out-Null #-Force } catch { Write-Error -Message "Unable to create directory '$DirectoryToCreate'. Error was: $_" -ErrorAction Stop } "Successfully created directory '$DirectoryToCreate'." } else { "Directory already existed" } ##Download Backup Script $backupurl="https://raw.githubusercontent.com/andrew-s-taylor/public/main/Batch%20Scripts/backup.bat" $backupscript = "c:\backup-restore\backup.bat" Invoke-WebRequest -Uri $backupurl -OutFile $backupscript -UseBasicParsing ##Download Restore Script $restoreurl="https://raw.githubusercontent.com/andrew-s-taylor/public/main/Batch%20Scripts/NEWrestore.bat" $restorescript = "c:\backup-restore\restore.bat" Invoke-WebRequest -Uri $restoreurl -OutFile $restorescript -UseBasicParsing ##Download Silent Launch Script $launchurl="https://raw.githubusercontent.com/andrew-s-taylor/public/main/Batch%20Scripts/run-invisible.vbs" $launchscript = "c:\backup-restore\run-invisible.vbs" Invoke-WebRequest -Uri $launchurl -OutFile $launchscript -UseBasicParsing ##Create scheduled task # Create a new task action $taskAction = New-ScheduledTaskAction -Execute 'c:\backup-restore\run-invisible.vbs' ##Create Trigger (login) $taskTrigger = New-ScheduledTaskTrigger -AtLogOn # Register the new PowerShell scheduled task #Name it $taskName = "UserBackup" #Describe it $description = "Backs up User profile to OneDrive" # Register it Register-ScheduledTask ` -TaskName $taskName ` -Action $taskAction ` -Trigger $taskTrigger ` -Description $description