In order to follow the 3-2-1 rule, I recently developed a small PowerShell script to copy backup files (I used it for VEEAM backups, but it works for any backup files) to a local USB disk. The aim is that the copy can be made by any user profile (from secretary to nuclear engineer).
The need is as follows: copy backup files from a remote server to a USB disk connected to a workstation that does not have pre-registered server credentials.
Let's see how it works in detail…
The aim here is to automatically retrieve the drive letter of our USB device. To do this, we'll define a specific label for this drive and our script will retrieve the letter associated to this label.
PS C:\ > $(Get-WmiObject Win32_LogicalDisk | Where-Object { $_.VolumeName -match "USB_BACKUP" }).DeviceID.ToString()
We're now going to generate a window box using the Get-Credential command, to allow the user to enter the server's credentials.
PS C:\ > $cred = Get-Credential
PS C:\ > $netcred = $cred.GetNetworkCredential()
PS C:\ > $pass = $netcred.Password
We're going to use the net use command with the previously retrieved credentials to connect to our backup server.
PS C:\ > net use \\BACKUP_SERVER_IP $netcred.Password /USER:$($cred.GetNetworkCredential().UserName)
The copy will be performed by the robocopy command. Let's take a look at the selected options.
PS C:\ > robocopy /MIR /R:0 /W:0 \\BACKUP_SERVER_IP\d$\Backup "$usb_drive\VEEAM"
Replace BACKUP_SERVER_IP with your current IP.
###########################
# author : shebangthedolphins.net
# version : 1.0
# date : 2021.03
# role : backup the backups to USB drive backup
# other : Tested on Windows 2019 Server
# updates :
# - 1.0 (2021/03) : First Version
#Get drive letter, exit if not found
$usb_drive = try { $(Get-WmiObject Win32_LogicalDisk | Where-Object { $_.VolumeName -match "USB_BACKUP" }).DeviceID.ToString() } catch { exit 1 }
#get credential window
$cred = Get-Credential
#get user and password credentials for net use command (see : https://stackoverflow.com/questions/612015/copy-item-with-alternate-credentials)
$netcred = $cred.GetNetworkCredential()
$pass = $netcred.Password
#connect to the BACKUP_SERVER_IP network resource
net use \\BACKUP_SERVER_IP $netcred.Password /USER:$($cred.GetNetworkCredential().UserName)
#mirror copy of files from the "\\BACKUP_SERVER_IP\\d$\Backup" folder to the "USB_DRIVE\VEEAM" folder
robocopy /MIR /R:0 /W:0 \\BACKUP_SERVER_IP\d$\Backup "$usb_drive\VEEAM"
#cancels network connection BACKUP_SERVER_IP
net use /delete \\BACKUP_SERVER_IP
Contact :