
SCOM Task to restart Agent – am i complicating…
As part of planning to migrate a large (3000 + VM) SCOM 2012 R2 environment to 2016 we will need to do a bit of agent configuration. I have created two tasks in Our SCOM console to help us add or delete management Groups from the monitored computers (when moving all agents we probably will use some other orchestration). The problem is that you will have to restart the agents HealthService to save new configuration. Since we execute the task/script through the Health service the task will fail when we attempt to restart. You may experience Your task to work, as in you will have the managment Group added, but you will not see any output.
Over the years People have published a lot of scripts that restarts the agents but unfortunately I have not managed to get them to work properly.
Therfore, i am complicating Things…
I created a script which creates a scheduled task set to run in one minute before it expires and deletes. The Whole thing Works perfectly, but there has to be another way?
<# NAME: Create-SCOMAgentRestartSchedTask.ps1 .DESCRIPTION Creates a scheduled task on the computer which will restart scom health service. Script is used as agent task within SCOM as a 'hack' to restart agent through the console. .NOTES Martin Ehrnst /Intility AS www.adatum.no Intial release November 16 Version 1.0 #> #Do some logging to the Operations Manager Event Log $api = new-object -comObject MOM.ScriptAPI $api.LogScriptEvent("Create-SCOMAgentRestartSchedTask.ps1", 1001, 4, "Creating a scheduled task to restart SCOM health service. Task will expire and delete after it's finished.") $Service = "HealthService" $Action = New-ScheduledTaskAction -Execute "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -Argument "Get-Service $Service | restart-service -force" $run = (Get-Date).AddMinutes(1) # Two minutes from now $User = 'SYSTEM' $trigger = New-ScheduledTaskTrigger -Once -At ($run) $settings = New-ScheduledTaskSettingsSet -DeleteExpiredTaskAfter 00:00:01 Register-ScheduledTask -TaskName "Restart SCOM HealthService" -User $user -InputObject ( ( New-ScheduledTask -Action $Action -Trigger $trigger -Settings $settings ) | %{ $_.Triggers[0].EndBoundary = $run.AddMinutes(5).ToString('s') ; $_ }) write-host "created a task to restart $service in one minute"
I know that Microsoft have a recovery action to restart the agent when it is using too much memory and similar, but I haven’t broken Down their code.
4 COMMENTS
Thank you very much for sharing, I learned a lot from your article. Very cool. Thanks. nimabi
Yea agree, I have a task that reissues certs to servers in other domains from our CA, restarts agent etc for the expiring schannel certs and you need to spawn a new process independent of the health service or the restart breaks when the agent process stops (most of the time).
the scheduled task gets around that but is more complicated than it needs to be.
Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.com/en/register?ref=P9L9FQKY
Hey,
I usually use a VBS script from Kevin Holman that creates separate WScript.Shell and executes the HealthService restart. from there. It works as expected.
'Begin Healthservice Restart
Set oShell = WScript.CreateObject("WScript.Shell")
set oShellEnv = oShell.Environment("Process")
computerName = oShellEnv("ComputerName")
'Echo that we are about to start for task output
WScript.echo "Beginning Restart attempt for HealthService"
strCommand = "cmd /c net stop HealthService && cmd /c net start HealthService"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")
Set objProgram = objProcess.Methods_( _
"Create").InParameters.SpawnInstance_
objProgram.CommandLine = strCommand
Set strShell = objWMIService.ExecMethod( _
"Win32_Process", "Create", objProgram)
'Echo that we are restarting for task output
WScript.echo "Restarting HealthService"
Comments are closed.