This PowerShell script appears to be designed to sync data from a local source path to an AWS S3 destination using the aws s3 sync
command. Before syncing, it checks if the script is running with administrative privileges. Let me break down what each section of the script does:
$syncPairs
: This is an array of hashtables, each representing a synchronization pair. Each hashtable has three keys:SourcePath
,DestinationPath
, andLogFilePath
. These key-value pairs specify the source directory, S3 destination, and the log file for each synchronization task.- Admin Privileges Check: The script checks whether it is running with administrative privileges by inspecting the current Windows user’s roles. If the script is not running as an administrator, it displays a message and exits.
foreach
Loop: This loop iterates over each synchronization pair defined in the$syncPairs
array.- Inside the Loop:
$sourcePath
: It represents the source directory from where data is to be synced.$destinationPath
: It represents the S3 destination where data will be synchronized.$logFilePath
: It specifies the path to a log file that will capture the output of theaws s3 sync
command.
- Sync Command: Inside the loop, the script uses the
aws s3 sync
command to synchronize data from the source directory ($sourcePath
) to the S3 destination ($destinationPath
). The>
operator is used to redirect the command’s output to the log file ($logFilePath
). - Log File: The log file is where you can find the detailed output of the synchronization process. It will contain information about the files transferred, any errors encountered, and the status of the synchronization.
In summary, this script is designed to automate the synchronization of data from local directories to AWS S3 using the AWS CLI (aws s3 sync
). It handles multiple synchronization tasks defined in the $syncPairs
array and logs the synchronization details to separate log files specified for each pair. This script is designed to be run with administrative privileges due to the potential need to access and modify file paths.
PowerShell script for Synchronizing your multiple folders with AWS s3
$syncPairs = @(
@{
SourcePath = "G:\userdata"
DestinationPath = "s3://infodata21/userdata"
LogFilePath = "c:\s3logs\userdata.txt"
},
@{
SourcePath = "G:\info_test"
DestinationPath = "s3://infodata21/info_test"
LogFilePath = "c:\s3logs\info_test.txt"
}
# Add more source and destination pairs as needed (Source and destination must be changed as per your infra and s3 bucket configuration)
)
# Check if the script is running with administrative privileges
$isAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
if (-not $isAdmin) {
Write-Host "This script requires administrative privileges. Please run it as an administrator."
exit
}
foreach ($pair in $syncPairs) {
$sourcePath = $pair.SourcePath
$destinationPath = $pair.DestinationPath
$logFilePath = $pair.LogFilePath
# Execute the AWS S3 sync command for each pair
aws s3 sync $sourcePath $destinationPath > $logFilePath
}