SCOM.Management.AddManagementGroup.WA (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.BaseData

Member Modules:

ID Module Type TypeId RunAs 
PSWA WriteAction Microsoft.Windows.PowerShellWriteAction Default

Overrideable Parameters:

IDParameterTypeSelector
MGNamestring$Config/MGName$
MSNamestring$Config/MSName$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="SCOM.Management.AddManagementGroup.WA" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="MGName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="MSName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="MGName" Selector="$Config/MGName$" ParameterType="string"/>
<OverrideableParameter ID="MSName" Selector="$Config/MSName$" ParameterType="string"/>
<OverrideableParameter ID="TimeoutSeconds" Selector="$Config/TimeoutSeconds$" ParameterType="int"/>
</OverrideableParameters>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="PSWA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>SCOM.Management.AddManagementGroup.WA.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Script to ADD a SCOM Management Group to an Agent
#=================================================================================
param([string]$MGName,[string]$MSName)


# Manual Testing section - put stuff here for manually testing script - typically parameters:
#=================================================================================
# $MGName = "SCOM TEST"
# $MSName = "SCOMServer1.domain.com"
#=================================================================================


# Constants section - modify stuff here:
#=================================================================================
# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.AddManagementGroup.WA.ps1"
$EventID = "1310"
#=================================================================================


# Starting Script section
#=================================================================================
# Gather who the script is running as
$whoami = whoami
#Load the MOMScript API and discovery propertybag
$momapi = New-Object -comObject "Mom.ScriptAPI"
#=================================================================================


# Begin Main Script
#=================================================================================
#Log event that we are starting task
$momapi.LogScriptEvent($ScriptName,$EventID,0, "`nStarting script. `nRunning as ($whoami)")
Write-Host "Task Starting. Running as ($whoami)"

# Check if this is running on a SCOM Management Server or Gateway and stop if it is
$SCOMServerRegKey = "HKLM:\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Server Management Groups"
IF (Test-Path $SCOMServerRegKey)
{
# This is a management server. STOP
$momapi.LogScriptEvent($ScriptName,$EventID,1,"`nFATAL ERROR: SCOM Server Role Detected. `nThis script should not run on Management Servers or Gateways `n Terminating script.")
Write-Host "`nFATAL ERROR: SCOM Server Role Detected. `nThis script should not run on Management Servers or Gateways `nTerminating script." -ForegroundColor Red
EXIT
}

#Load agent scripting object
$AgentCfg = New-Object -ComObject AgentConfigManager.MgmtSvcCfg

Write-Host "Adding Management Group ($MGName)"
$Error.Clear()

TRY
{
$AgentCfg.AddManagementGroup("$MGName","$MSName",5723)
}
CATCH
{
Write-Host "ERROR adding Management group. Error is: ($Error)."
EXIT
}

Write-Host "Management Group ($MGName) Added."
#Restart Agent
Write-Host "Restarting Agent now...."
#=================================================================================
# End Main Script

# Restart Agent
#=================================================================================
#We need a reliable way to restart the SCOM Agent out of band so that tasks can complete with success
$Command = "Start-Sleep -s 5;Restart-Service HealthService"
$Process = ([wmiclass]"root\cimv2:Win32_ProcessStartup").CreateInstance()
$Process.ShowWindow = 0
$Process.CreateFlags = 16777216
([wmiclass]"root\cimv2:Win32_Process").Create("powershell.exe $Command")|Out-Null
#=================================================================================
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>MGName</Name>
<Value>$Config/MGName$</Value>
</Parameter>
<Parameter>
<Name>MSName</Name>
<Value>$Config/MSName$</Value>
</Parameter>
</Parameters>
<TimeoutSeconds>$Config/TimeoutSeconds$</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="PSWA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>