Microsoft.SQLServer.Windows.RunAs.Addendum.SetHSServiceSidStateandRestart.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

Source Code:

<WriteActionModuleType ID="Microsoft.SQLServer.Windows.RunAs.Addendum.SetHSServiceSidStateandRestart.WA" Accessibility="Internal" Batching="false">
<Configuration/>
<ModuleImplementation Isolation="Any">
<Composite>
<MemberModules>
<WriteAction ID="PSWA" TypeID="Windows!Microsoft.Windows.PowerShellWriteAction">
<ScriptName>Microsoft.SQLServer.Windows.RunAs.Addendum.SetHSServiceSidStateandRestart.WA.ps1</ScriptName>
<ScriptBody><Script>
#=================================================================================
# Script to enable HealthServiceSID and Restart SCOM Agent
#
# Author: Kevin Holman
# v1.2
#=================================================================================


# Constants section - modify stuff here:
#=================================================================================
# Assign script name variable for use in event logging.
$ScriptName = "Microsoft.SQLServer.Windows.RunAs.Addendum.SetHSServiceSidStateandRestart.WA.ps1"
$EventID = "8700"
#=================================================================================


# Starting Script section - All scripts get this
#=================================================================================
#Set variable to be used in logging events
$whoami = whoami
# Load MOMScript API
$momapi = New-Object -comObject MOM.ScriptAPI
#=================================================================================


# Begin MAIN script section
#=================================================================================
#Log script event that we are starting task
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nScript is starting. `nRunning as ($whoami). `nAttempting to enable SID for Healthservice.")
Write-Host "Script is starting. `nRunning as ($whoami). `nAttempting to enable SID for Healthservice."

[string]$Command = "sc sidtype HealthService unrestricted"
$ExecCommandOutput = cmd /c $Command
$momapi.LogScriptEvent($ScriptName,$EventID,0,"`nOutput of attempt to configure HealthService SID Type is: ($ExecCommandOutput). Restarting SCOM agent now...")
Write-Host $ExecCommandOutput
Write-Host "Restarting SCOM agent now..."
#=================================================================================
# End MAIN script section

# 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>
<TimeoutSeconds>120</TimeoutSeconds>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="PSWA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.BaseData</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>