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:

  1. $syncPairs: This is an array of hashtables, each representing a synchronization pair. Each hashtable has three keys: SourcePath, DestinationPath, and LogFilePath. These key-value pairs specify the source directory, S3 destination, and the log file for each synchronization task.
  2. 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.
  3. foreach Loop: This loop iterates over each synchronization pair defined in the $syncPairs array.
  4. 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 the aws s3 sync command.
  5. 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).
  6. 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
}

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *