logo

NJP

IntegrationHub - Windows Service Restart

Import · Oct 30, 2020 · article

Using IntegrationHub in Flow Designer with a PowerShell Step you can Start | Stop | Restart a Windows Service remotely.

Prerequisites:

Add to the Windows CI Classification the Probe "WMIRunner-Windows - Services" in the "Triggers probes" related list as shown in the image below.

Go to: Discovery Definition > CI Classification > Windows and select the version of Windows you want to configure.

image

Windows Service Action

Using Flow Designer create a new custom Action with the following configuration.

Inputs:

image

PowerShell Step

image

Outputs:

image

PowerShell Script

<#
  This script is used to Start, Stop or Restart a Windows Service
  Developed by: Oscar Lopez
  oscar.lopez@ittroopers.com
  @oslovanet

> # Get current data/time stamp

function getDT()
{ (Get-Date -Format "yyyy-MM-dd HH:mm:ss ")
} # Checks if Service exists
function existService($ServiceName, $ComputerName) { Get-Service -Name $ServiceName -ComputerName $ComputerName -ErrorAction SilentlyContinue
} # Gets the Service
function getService($ServiceName, $ComputerName) { Get-Service -Name $ServiceName -ComputerName $ComputerName
} # Validates if Service exists and provides Service Status
function getServiceStatus ($ServiceName, $ComputerName) { if ( (existService $ServiceName $ComputerName) ) { $ServiceStatus = (getService $ServiceName $ComputerName).Status Write-Host (getDT) $ServiceName "-" $ServiceStatus } else { Write-Host (getDT) "$ServiceName not found" }
} # returns true when Service is Running
function isServiceRunning($ServiceName, $ComputerName) { (getService $ServiceName $ComputerName).Status -eq 'Running'
} #returns true when Service is Stopped
function isServiceStopped($ServiceName, $ComputerName) { (getService $ServiceName $ComputerName).Status -eq 'Stopped'
} Write-Host (getDT) "Initiating $ServiceAction of the Service $ServiceName hosted by $ComputerName" # Validates if Service Name exists
if (existService $ServiceName $ComputerName) { # STOP condition if ($ServiceAction -eq 'Stop') { if ( (isServiceRunning $ServiceName $ComputerName) ) { Write-Host (getDT) $ServiceName "is running, preparing to stop..." getService $ServiceName $ComputerName | Stop-Service -ErrorAction SilentlyContinue getServiceStatus $ServiceName $ComputerName } elseif ( (isServiceStopped $ServiceName $ComputerName) ) { Write-Host (getDT) $ServiceName "already stopped!" } else { Write-Host (getDT) $ServiceName "-" $ServiceStatus } } # START condition elseif ($ServiceAction -eq 'Start') { if ( (isServiceRunning $ServiceName $ComputerName) ) { Write-Host (getDT) $ServiceName "already running!" } elseif ( (isServiceStopped $ServiceName $ComputerName) ) { Write-Host (getDT) $ServiceName " is stopped, preparing to start..." getService $ServiceName $ComputerName | Start-Service -ErrorAction SilentlyContinue getServiceStatus $ServiceName $ComputerName } else { Write-Host (getDT) $ServiceName "-" $ServiceStatus } } # RESTART condition elseif ($ServiceAction -eq 'Restart') { if ( (isServiceRunning $ServiceName $ComputerName) ) { Write-Host (getDt) $ServiceName "is running, preparing to restart..." getService $ServiceName $ComputerName | Stop-Service -ErrorAction SilentlyContinue getServiceStatus $ServiceName $ComputerName getService $ServiceName $ComputerName | Start-Service -ErrorAction SilentlyContinue getServiceStatus $ServiceName $ComputerName } elseif ( (isServiceStopped $ServiceName $ComputerName) ) { Write-Host (getDT) $ServiceName "is stopped, preparing to start..." getService $ServiceName $ComputerName | Start-Service -ErrorAction SilentlyContinue getServiceStatus $ServiceName $ComputerName } } #Condition if action is anything other than stop, start, restart else { Write-Host (getDT) "Action parameter is missing or invalid!" }
} #Condition if provided ServiceName is invalid
else
{ Write-Host (getDT) "$ServiceName not found"
}

Leave your comments and mark helpful if you fin this article useful.

Enjoy!!!

@oslovanet

View original source

https://www.servicenow.com/community/now-platform-articles/integrationhub-windows-service-restart/ta-p/2322478