Private/ServiceUtil.ps1

# Determines if a Service exists with a name as defined in $ServiceName.
# Returns a boolean $True or $False.
Function Test-ServiceExists([string] $ServiceName) {
    [bool] $Return = $False
    if ( Get-CimInstance -ClassName Win32_Service -Filter "Name='$ServiceName'" ) {
        $Return = $True
    }
    Return $Return
}

# Deletes a Service with a name as defined in $ServiceName.
# Returns a boolean $True or $False. $True if the Service didn't exist or was
# successfully deleted after execution.
Function Remove-Service([string] $ServiceName) {
    [bool] $Return = $False
    $Service = Get-CimInstance -ClassName Win32_Service -Filter "Name='$ServiceName'" 
    if ( $Service ) {
        Remove-CimInstance -InputObject $Service
        if ( -Not ( Test-ServiceExists $ServiceName ) ) {
            $Return = $True
        }
    } else {
        $Return = $True
    }
    Return $Return
}

function Get-PlanningVersion($version) {
    $vstr = "NA | NA | NA"
    if ($version -ne $null)
    {
        $V = $version.Version
        $S = $version.StaticDataVersion
        $D = $version.DynamicDataVersion
        $vstr = "$V | $S | $D"
    }

    return $vstr
}

function Get-PlanningVersionWeb($version) {
    $vstr = "NA`t| NA`t| NA"
    if ($version -ne $null)
    {
        $V = $version.Planning
        $S = $version.Static
        $D = $version.Dynamic
        $vstr = "$V`t| $S`t| $D"
    }

    return $vstr
}

function Stop-ServiceAndProcess([string] $ServiceName) {

    Write-Verbose "Looking for service $ServiceName"
    $id = Get-WmiObject Win32_Service -Filter "Name LIKE '$ServiceName'" | Select-Object -expand ProcessId

    if ($id -ne 0) {
        Write-Verbose "Found service with process ID: $id"
        Stop-Service -Name $ServiceName -Force

        Write-Verbose "Wait for complete stop."
        Start-Sleep -s 10

        if (Get-Process -Id $id -ErrorAction SilentlyContinue) {
            Stop-Process -Id $id -Force -Confirm:$false
            Write-Warning "Process has been stopped (forced)."
        } else {
            Write-Verbose "Service stopped normaly."
        }

    } else {
        Write-Verbose "No service found for the given instance."
    }
}