Public/New-ServiceObject.ps1

#Used By New-P2PTunnel

Function New-ServiceObject {
    <#
    .Description
    Create a new Service Object
 
    .Parameter ServiceName
    Specify a Unique name for the Service Object
 
    .Parameter TCPPortRange
    Specify the TCP port or port range
 
    .Parameter UDPPortRange
    Specify the UDP port or port range
 
 
    .Example
    $Params = @{
        ServiceName = "OWA"
        TCPPortRange = "443"
    }
 
    New-ServiceObject @params
 
    .Example
    If you require a range of ports specify the parameter range like shown here.
 
    $Params = @{
        ServiceName = "RTP"
        UDPPortRange = "10000-11000"
    }
 
    New-ServiceObject @params
 
    .Example
    This example generates an SSH session and invokes the output of this function against that session.
 
    New-SSHSession -computername 192.168.0.1
 
    $Params = @{
        ServiceName = "RTP"
        UDPPortRange = "10000-11000"
    }
    $command = New-ServiceObject @params
 
    $result = Invoke-SSHCommand -Command $command -SessionId 0
    $result.output
 
    .Example
    This example generates multiple SSH sessions and invokes the output of this function against all active sessions.
 
    New-SSHSession -computername 192.168.0.1
    New-SSHSession -computername 192.168.1.1
 
    $Params = @{
        ServiceName = "RTP"
        UDPPortRange = "10000-11000"
    }
    $command = New-ServiceObject @params
 
    $sessions = Get-SSHSession
    foreach ($session in $sessions) {
        Write-Output "Invoking Command against $session.host"
        $result = Invoke-SSHCommand -Command $command -SessionId $session.sessionID
        $result.output
    }
 
    .Link
    https://github.com/TheTaylorLee/AdminToolbox/tree/master/docs
    #>


    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true, ParameterSetName = 'TCP')]
        [Parameter(Mandatory = $true, ParameterSetName = 'UDP')]$ServiceName,
        [Parameter(Mandatory = $true, ParameterSetName = 'TCP')]$TCPPortRange,
        [Parameter(Mandatory = $true, ParameterSetName = 'UDP')]$UDPPortRange
    )

    if ($TCPPortRange) {
        Write-Output "
config firewall service custom
    edit ""$ServiceName""
        set tcp-portrange $TCPPortRange
    next
end"

    }

    if ($UDPPortRange) {
        Write-Output "
config firewall service custom
    edit ""$ServiceName""
        set udp-portrange $UDPPortRange
    next
end"

    }
}