# 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>