Blog

SCOM: Agentpostinstall.ps1 PowerShell demo script Webcast 01042014

On april 1st 2014 (a day I will remember for a long time due to various reasons) I held a webcast for Microsoft Technet Belux regarding automation of admin tasks in SCOM.

I went over the basics to get started, the pitfalls and gave some tips and tricks to get you going. This session was recorded and together with the slide deck it’s made available here:

http://www.slideshare.net/technetbelux/make-scom-work-for-you-and-not-the-other-way-around

In this demo I created a small PowerShell script that could save you some time when agents are installed in your environment through an image. In this particular scenario the agents are automatically in the “pending approval” list in SCOM.

Running this PowerShell will add them to the environment, make them remotely manageable, point them all to a management server of your choice and put agent proxying on true.

Feel free to adapt the script for your needs.

The script in question:

[xml]

#=====================================================================================================
# AUTHOR:    Dieter Wijckmans
# DATE:        01/04/2014
# Name:        agentpostinstall.PS1
# Version:    1.0
# COMMENT:    Approve agents after install, make remotely manageable, assign to 1 management server
#           and enable agent proxying.
#
# Usage:    .\postinstallagenttasks.ps1 mgserverfrom mgserverto sqlserverinstance dbase
# Parameters: mgserverfrom: the primary server at this point
#             mgserverto: The new primary server
#             sqlserverinstance: the sql server where the opsdb resides + instance
#             dbase: name of the opsdb
#
#=====================================================================================================

param ([string]$mgserverfrom,[string]$mgserverto,[string]$sqlserverinstance,[string]$dbase)
###Prepare environment for run###

####
# Start Ops Mgr snapin
###

##Read out the Management server name
$objCompSys = Get-WmiObject win32_computersystem
$inputScomMS = $objCompSys.name

#Initializing the Ops Mgr 2012 Powershell provider#
Import-Module -Name "OperationsManager"
New-SCManagementGroupConnection -ComputerName $inputScomMS

#Get all agents which are in pending mode and approve
$pending = Get-SCOMPendingManagement | Group AgentPendingActionType
$Count = $pending.count
echo $count

If ($count -eq $null)
{
echo "No agents to approve"
Exit
}
Else
{
Get-SCOMPendingManagement | where {$_.AgentPendingActionType -eq "ManualApproval"} | Sort AgentName | Approve-SCOMPendingManagement
}

#Let all servers report to 1 primary management server

$serverfrom = Get-SCOMManagementServer | ? {$_.name -eq "$mgserverfrom"}
$agents = Get-SCOMAgent -ManagementServer $serverfrom
$serverto = Get-SCOMManagementServer | ? {$_.name -eq "$mgserverto"}
Set-SCOMParentManagementServer -Agent:$agents -FailoverServer:$null
Set-SCOMParentManagementServer -Agent:$agents -PrimaryServer:$serverto
Set-SCOMParentManagementServer -Agent:$agents -FailoverServer:$serverfrom

#Set all servers to remotely manageable in SQL

$ServerName = "$sqlserverinstance"
$DatabaseName = "$dbase"
$Query = "UPDATE MT_HealthService SET IsManuallyInstalled=0 WHERE IsManuallyInstalled=1"

#Timeout parameters
$QueryTimeout = 120
$ConnectionTimeout = 30

#Action of connecting to the Database and executing the query and returning results if there were any.
$conn=New-Object System.Data.SqlClient.SQLConnection
$ConnectionString = "Server={0};Database={1};Integrated Security=True;Connect Timeout={2}" -f $ServerName,$DatabaseName,$ConnectionTimeout
$conn.ConnectionString=$ConnectionString
$conn.Open()
$cmd=New-Object system.Data.SqlClient.SqlCommand($Query,$conn)
$cmd.CommandTimeout=$QueryTimeout
$ds=New-Object system.Data.DataSet
$da=New-Object system.Data.SqlClient.SqlDataAdapter($cmd)
[void]$da.fill($ds)
$conn.Close()
$ds.Tables

#Set all servers to agent proxy enabled

Get-SCOMAgent | where {$_.ProxyingEnabled.Value -eq $False} | Enable-SCOMAgentProxy

[/xml]

It can be downloaded here

download-button-fertig11

Note

  • that you need to give the proper parameters for it to work as stated in the description.
  • that perhaps you will have to check the SQL connection string on-line 68 with your SQL dba and adapt accordingly.

Enough talk, let’s build
Something together.