Thursday, October 19, 2017

#OpsMgr: Bulk deployment of Linux agents via a script

One of my customers had a requirement to deploy in excess of 2000 Linux agents recently, and was looking for a streamlined way of doing this. I found a couple of scripts online that can help with the deployment of the agent, but none that did quite what we needed to do, so I wrote my own.

Pop the list of agents to deploy into a text file and update the script to point to that file. You can also specify the name of the resource pool that should manage these agents. The script will prompt you for the run-as account username and password when you run it, so this is probably not ready to run unattended.

Script pings agent first to make sure it is reachable, and will then attempt to deploy/configure the agent, the same way the discovery does in the console.

Script results are saved into a CSV file, which is also mailed to you.

Update the bits highlighted in yellow before running. I would also recommend doing to pasting via notepad thing to ensure no mangling of quotation marks, etc happen.


#################################################################
#
#   SCOM-Linux-Agent-Discovery-script.ps1
#
#   Script by: Vanessa Bruwer
#
#
#    This script initiates the discovery for a list of Linux servers from a text file.
#    It is to be used for a set of servers to be managed by the Resource Pool
#    specified in this script.
#
#    The script does some prechecking before the server is added to the discovery.
#
#    It is based on the script provided by Operating Quadrant here:
#    https://operatingquadrant.com/2012/12/06/using-powershell-for-automated-unixlinux-agent-discovery/
#
#################################################################

# Parameters

$UserName = Read-Host -Prompt 'Please enter the Linux username'
$Password = Read-Host -Prompt 'Please enter the Linux password'


# Variables

$ResourcePoolName = "Linux Resource Pool"
$AgentFileLoc = "c:\temp\scripts\linuxagentlist.txt"

$sleeptime = "60"      # sleep time in seconds



$FromEmail = ""
$ToEmail = ""
$MailServer = ""


$ReportDate = Get-Date -format "yyyy-M-dd"

$rnd = Get-Random -minimum 1 -maximum 1000
$ReportLocation = "C:\Temp"
$ReportName = "$ReportDate-$rnd-SCOM-linuxagentdiscovery.csv"
$SavetoFile = "$ReportLocation\$ReportName"



# there should be no need to update the script beyond this point


# Delete any previous versions of the report for in case

If (Test-Path $SavetoFile){
     Remove-Item $SavetoFile
}

# Open the content
$body = @()

$body += "Server Name,Ping Response,DiscoveryResult"


# Read the text file
$Agents = get-content $AgentFileLoc


# Connect to Operations Manager

Import-module OperationsManager

$wsmanuser=$UserName
$wsmanpassword = ConvertTo-SecureString $Password -AsPlainText -Force


$WSCredential = New-Object System.Management.Automation.PSCredential ($wsmanuser, $wsmanpassword)
$Pool = Get-SCOMResourcePool -DisplayName $ResourcePoolName

$SSHCredential = Get-SCXSSHCredential -username $UserName –ElevationType sudo

# Set the counters to Zero
$i = 0
$n = 0

# Loop through the pending agents and ping them
foreach ($Agent in $Agents) {

$n = $n+1

   if (test-Connection -ComputerName $Agent -Count 2 -Quiet )
         {
     $Status = "Responds"

    $DiscResult = Invoke-SCXDiscovery -name $Agent -ResourcePool $Pool -WsManCredential $WSCredential -SshCredential $SSHcredential

    $DiscResult | fl Succeeded, ErrorData
     $DiscResult | Install-scxagent

if ($DiscResult.Succeeded){

$DiscStatus = "Success"
}
elseif ($DiscResult.EndpointAlreadyManaged)
{
$DiscStatus = "Skipped, already managed"
}
elseif ($DiscResult.ErrorData){
$DiscStatus = "Error occurred, please investigate"
}


Start-Sleep -s $sleeptime

         }
          else
          {
       $Status = "Unreachable"
       $i = $i+1

      $DiscStatus = "Skipped"
          }


# Write to the report
$ReportOutput = "$Agent,$Status,$DiscStatus"
$body += $ReportOutput
}


$agentcnt = $n-$i

$body >> $SavetoFile

$mbody = "There are $n Linux agents in the text file, $i not pinging and $agentcnt added to discovery"

Write-Host "There are $n Linux agents in the text file, $i not pinging and $agentcnt added to discovery"

Write-Host “Sending Email”


  #Creating a Mail object
      $msg = new-object Net.Mail.MailMessage
      $att = New-Object Net.Mail.Attachment($SavetoFile)

#Creating SMTP server object
      $smtp = new-object Net.Mail.SmtpClient($mailserver)

#Email structure
  $Subject = "SCOM Linux Agent discovery - $ReportDate"

     $msg.From = $FromEmail
      $msg.ReplyTo = $FromEmail
      $msg.To.Add($ToEmail)
      $msg.subject = $Subject
      $msg.body = $mbody
      $msg.Attachments.Add($att)


   #Sending email
     $smtp.Send($msg)

#write-host "Email sent"


And, as always, test in lab before using in production.

No comments:

Related Posts with Thumbnails