code-screen

How to install SSM agent on multiple EC2 instances

PowerShell Script to Install SSM Agent

1. Prepare CSV File: Create a CSV file containing a list of server names or IP addresses where you want to install the SSM agent. Save this file with a suitable name, for example, servers.csv. Ensure that the CSV file has a header (e.g., ServerName).

2. Install SSM Agent Script: Write a PowerShell script that reads the CSV file, iterates through each server, and installs the SSM agent.

Below is the PowerShell script that accomplishes this task:

 # Read server names from CSV file
$servers = Import-Csv -Path "C:\Path\to\servers.csv"

# Loop through each server and install SSM agent
foreach ($server in $servers) {
    $serverName = $server.ServerName
    Write-Host "Installing SSM agent on $serverName"
    
    # Use PowerShell remoting to install SSM agent on the remote server
    Invoke-Command -ComputerName $serverName -ScriptBlock {
        # Download SSM agent installer
        Invoke-WebRequest -Uri "https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/windows_amd64/AmazonSSMAgentSetup.exe" -OutFile "$env:TEMP\AmazonSSMAgentSetup.exe"
        
        # Install SSM agent
        Start-Process -FilePath "$env:TEMP\AmazonSSMAgentSetup.exe" -ArgumentList "/S" -Wait
    }
    
    Write-Host "SSM agent installed on $serverName"
}

Replace "C:\Path\to\servers.csv" with the actual path to your CSV file.

Note: Ensure that you have appropriate permissions and network access to remotely install software on the target servers. Test the script in a non-production environment before deploying it to production servers.


Comments

Leave a Reply

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