SCOM.Management.InstallAgent.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
AgentNamestring$Config/AgentName$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="SCOM.Management.InstallAgent.WA" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="AgentName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="AgentName" Selector="$Config/AgentName$" 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.InstallAgent.WA.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Script to Push Install SCOM agents via PowerShell
#
# Takes a single parameter of a computer FQDN
# Should be run on a management server
#
# v 1.0
#=================================================================================
param($AgentName)

# For testing manually in PowerShell:
# $AgentName = 'WS2012.opsmgr.net'

#=================================================================================
# Constants section - modify stuff here:

# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.InstallAgent.WA.ps1"
#=================================================================================

# Gather script start time
$StartTime = Get-Date

# Gather who the script is running as
$whoami = whoami

#Load the MOMScript API and discovery propertybag
$momapi = New-Object -comObject "Mom.ScriptAPI"

#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,1017,0, "Starting script. AgentName is ($AgentName). Running as ($whoami)")

#Connect to local SCOM Management Group Section
#=================================================================================
# Clear any previous errors
if($Error)
{
$Error.Clear()
}

# Import the OperationsManager PowerShell module and connect to the management group
Try
{
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
Import-module $SCOMModulePath
}
Catch
{
$momapi.LogScriptEvent($ScriptName,1017,2, "Unable to load the OperationsManager module, Error is: $error")
}
Try
{
New-DefaultManagementGroupConnection
}
Catch
{
$momapi.LogScriptEvent($ScriptName,1017,2, "Unable to connect to the management server. Error when calling New-DefaultManagementGroupConnection. Error is: $error")
}
#=================================================================================


# Begin Main Script
#=================================================================================
# Get local SCOM management server name
$LocalHost = [System.Net.Dns]::GetHostEntry([string]$env:computername).HostName
Write-Host "Running on management server ($LocalHost)"
Write-Host "Getting Management Server object"
# Get SCOM Management Server object
$PrimaryMgmtServer = Get-SCOMManagementServer -Name $LocalHost
Write-Host "Attempting to Install Agent"
# Clear any previous errors
if($Error)
{
$Error.Clear()
}
# Install SCOM Agent
Install-SCOMAgent -DNSHostName $AgentName -PrimaryManagementServer $PrimaryMgmtServer
if($Error)
{
Write-Host "Error ocurred: ($Error)"
$momapi.LogScriptEvent($ScriptName,1017,2, "Error installing agent. Error is: $Error")
}
Write-Host "Push install attempted. Check Alerts view for any failures, Pending Actions, or the Agent Logs on the management servers."
#=================================================================================
# End Main Script


# Log an event for script ending and total execution time.
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
$momapi.LogScriptEvent($ScriptName,1017,0,"`n Script has completed. Runtime is ($ScriptTime).")
</Script></ScriptBody>
<Parameters>
<Parameter>
<Name>AgentName</Name>
<Value>$Config/AgentName$</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>