Functions/Set-BcServerInstance.ps1

<#
.SYNOPSIS
    Start or stops services of specified Business Central Server instance.
.DESCRIPTION
    Use the Set-FpsBCServerInstance cmdlet to Start or stop all of the specified Business Central Server instance.
     
.EXAMPLE
    Set-FpsBCServerInstance -Computer 'remote.domain.eu' -ServiceName 'Nav$150' -Runstate Stop
.EXAMPLE
    Get-FpsBCServerInstance -Computer 'remote.domain.eu' -ServiceInstance 'BC150' | Set-FpsBCServerInstance -Runstate Start
 
.EXAMPLE
    # Stop all BC 15 service instances with a running service state, excluding the BC150 service.
    Get-FpsBCServerInstance -VersionFilter '15' -StateFilter Running -ExcludeServerInstance 'BC150' -Computer 'remote.domain.eu' -ServiceInstance 'BC150' | Set-FpsBCServerInstance -Runstate Running
#>

function Set-BCServerInstance {
    [CmdletBinding()]
    Param (
        # Param1 help description
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string[]] $ServiceName,

        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string] $Computer = $env:COMPUTERNAME,
        
        [Parameter(ValueFromPipelineByPropertyName = $true)]
        [string] $State,

        [Parameter(Position = 0, Mandatory=$true, ParameterSetName='All Instances')]
        [ValidateSet("Start", "Stop", "Restart")]
        [string] $Runstate

    )
    
    begin {
        
        $Results = @()

        [scriptblock] $ScriptBlock = {
            param(
                $ServiceName,
                $State,
                $Runstate
            )

            Get-Service -Name $ServiceName

            switch ($Runstate) 
            {
                'Start' {
                    Start-Service -Name $ServiceName
                }
                'Stop' {
                    Stop-Service -Name $ServiceName -Force
                }
                'Restart' {
                    Stop-Service -Name $ServiceName -Force
                    Start-Service -Name $ServiceName
                }

            }
            $Results = "Done: $ServiceName, $State, $Runstate"
            return $Results
        }
    }
    
    process {
        
        $AdditionParams = @{}
        if ($Computer -ne 'localhost' -and $Computer -ne $env:COMPUTERNAME) {
            $AdditionParams = @{'ComputerName' = $Computer}
        }
        
        $message = "{0} {1} {2} {3} " -f $Computer, $ServiceName[0], $State, $Runstate
        Write-Output $message

        $Result = Invoke-Command @AdditionParams -ScriptBlock $ScriptBlock -ArgumentList `
            $ServiceName[0],
            $State,
            $Runstate

    }
    end {
        return $Result
    }
}

Export-ModuleMember -Function Set-BCServerInstance


# TESTING

#Get-BCServerInstance -Computer fdb01 | Set-BCServerInstance Stop
#Get-BCServerInstance -StateFilter Running | Set-BCServerInstance Stop
#Get-BCServerInstance -Computer fdb01 -ServerInstance TestNL_Spring20 | Set-BCServerInstance -Runstate Stop

# Get-BCServerInstance -Computer fdb01 -StartModeFilter Manual -StateFilter Running|ft