SCOM.Management.RestartService.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
ServiceNamestring$Config/ServiceName$
TimeoutSecondsint$Config/TimeoutSeconds$

Source Code:

<WriteActionModuleType ID="SCOM.Management.RestartService.WA" Accessibility="Internal" Batching="false">
<Configuration>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="ServiceName" type="xsd:string"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" minOccurs="1" name="TimeoutSeconds" type="xsd:integer"/>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="ServiceName" Selector="$Config/ServiceName$" 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.RestartService.WA.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Script to restart services via PowerShell
#=================================================================================
param($ServiceName)

# For testing discovery manually in PowerShell:
# $ServiceName = "xspooler"

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

# Assign script name variable for use in event logging
$ScriptName = "SCOM.Management.RestartService.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,1315,0, "Starting script. Running as ($whoami)")

# Begin Main Script
#=================================================================================
$Services = Get-Service $ServiceName
IF (!$Services)
{
#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,1315,2, "Service ($ServiceName) not found. Error is ($error)")
EXIT
}
ELSE
{
Write-Host "Attempting to restart service: " $ServiceName
Restart-Service $Services -PassThru
}

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