New
#1
What's the best way to achieve this automated backup?
To preface: I've already been doing this via Macrium however it's kinda uncomfortable or not optimal because I have to mount the folder backup as a virtual drive to access its contents.
So because of this, I've written the following script:
Ideally the event trigger would be the plugging of a USB flash drive into the PC but all the articles I have found regarding that seem to make it an even less desirable way to get this workingCode:Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass $Source = "C:\Users\(USERNAME)\Scripts" Import-Module -Name BurntToast function ShowNotification { $NotificationMessage = "All files copied from $Source to $Destination" New-BurntToastNotification -Text $NotificationMessage } function CopyFiles { Write-Host "Copying files from $Source to $Destination" $sourceFiles = Get-ChildItem -Path $Source -File -Recurse foreach ($sourceFile in $sourceFiles) { $destinationFile = Join-Path -Path $Destination -ChildPath $sourceFile.FullName.Substring($Source.Length + 1) if (!(Test-Path -Path $destinationFile) -or $sourceFile.LastWriteTime -gt (Get-Item -Path $destinationFile).LastWriteTime) { Copy-Item -Path $sourceFile.FullName -Destination $destinationFile -Force Write-Host "Copied " -NoNewline Write-Host "$($sourceFile.FullName)" -ForegroundColor Red } else { Write-Host "Skipped " -NoNewline Write-Host "$($sourceFile.FullName)" -ForegroundColor Green Write-Host " (already up to date)" } } Write-Host "Copying complete." ShowNotification # Display the notification } while ($true) { # Check if USB drive is connected $usbDrive = Get-WmiObject Win32_Volume | Where-Object { $_.DriveType -eq 2 -and $_.Label -eq "USB-C-Drive" } if ($usbDrive) { $Destination = Join-Path -Path $usbDrive.DriveLetter -ChildPath "downloads test mappa" Write-Host "USB drive found. Copying files..." CopyFiles } else { Write-Host "USB drive not found. Waiting..." } Start-Sleep -Seconds 10 }
Is there a way to do this in a simpler, more comfortable manner? The script works, I'll even refine it if needs be, but I need to make this trigger event more simpler and convenient.