SCOM.Management.ApprovePendingAction.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.ApprovePendingAction.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.ApprovePendingAction.WA.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Script to Approve Pending Actions
#
# This script will approve a pending action based on passing the agent name
# Takes a single parameter of a computer FQDN
#
# v 1.0
#=================================================================================
param($AgentName)


# Manual Testing section - put stuff here for manually testing script - typically parameters:
#=================================================================================
# $AgentName = 'WS2012R2.opsmgr.net'
#=================================================================================


# Constants section
#=================================================================================
# Assign script name variable for use in event logging
# ScriptName should be the same as the ID of the module that the script is contained in
$ScriptName = "SCOM.Management.ApprovePendingAction.WA.ps1"
$EventID = "1093"
#=================================================================================


# Starting Script section - All scripts get this
#=================================================================================
# Gather the start time of the script
$StartTime = Get-Date
#Set variable to be used in logging events
$whoami = whoami
# Load MOMScript API
$momapi = New-Object -comObject MOM.ScriptAPI
#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,$EventID,0, "Starting script. `nAgentName is ($AgentName). `nRunning as ($whoami).")
Write-Host "Starting script to approve Pending Action. `nAgentName is ($AgentName). `nRunning as ($whoami)."
#=================================================================================


# Connect to local SCOM Management Group Section - If required
#=================================================================================
# I have found this to be the most reliable method to load SCOM modules for scripts running on Management Servers
# Clear any previous errors
$Error.Clear()
# Import the OperationsManager module and connect to the management group
$SCOMPowerShellKey = "HKLM:\SOFTWARE\Microsoft\System Center Operations Manager\12\Setup\Powershell\V2"
$SCOMModulePath = Join-Path (Get-ItemProperty $SCOMPowerShellKey).InstallDirectory "OperationsManager"
Import-module $SCOMModulePath
TRY
{
New-DefaultManagementGroupConnection -managementServerName "localhost"
}
CATCH
{
IF ($Error)
{
$momapi.LogScriptEvent($ScriptName,$EventID,1,"`n FATAL ERROR: Unable to load OperationsManager module or unable to connect to Management Server. `n Terminating script. `n Error is: ($Error).")
EXIT
}
}
#=================================================================================


# Begin Main Script
#=================================================================================
$Error.Clear()
$Pending = Get-SCOMPendingManagement | where {$_.agentname -eq $AgentName}
IF ($Pending)
{
Write-Host "Found a Pending Action for ($AgentName). `nAttempting Approval now."
$Pending | Approve-SCOMPendingManagement
IF ($Error)
{
Write-Host "Error attempting approval. `nError is ($Error)"
}
ELSE
{
Write-Host "Successfully approved pending action for ($AgentName)."
}
}
ELSE
{
Write-Host "Unable to find a Pending Action for ($AgentName). `nTerminating."
}
#=================================================================================
# End Main Script


# Log an event for script ending and total execution time.
$EndTime = Get-Date
$ScriptTime = ($EndTime - $StartTime).TotalSeconds
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nScript has completed. `nRuntime is ($ScriptTime) seconds.")
</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>