# encoding: ascii
# api: powershell
# title: Set-ESXRemoteCLI
# description: This script enables or disables the remote CLI services on a host. It accepts multiple hosts through the pipeline, useful for enabling or disabling SSH on several hosts at once.
# version: 0.1
# type: function
# author: jgrote
# license: CC0
# function: Set-ESXRemoteCLI
# x-poshcode-id: 3292
# x-archived: 2012-03-28T09:04:48
# x-published: 2012-03-17T11:55:00
#
# Usage Example: get-vmhost | set-esxremotecli -enabled $true -onboot
#

function Set-ESXRemoteCLI() {
    Param([parameter(Mandatory=$true,ValueFromPipeline=$true)]$VMHost,
        [parameter(Mandatory=$true)][Boolean]$enabled,
        [switch]$onboot
    )
    
    Process {
        $VMHost | foreach {
            write-progress -id 1 -activity "Modifying Remote CLI on $VMHost" -status "Accessing Remote CLI services on $VMHost"
            $CLIservice = get-vmhostservice $_ | where {$_.key -match "TSM*"}
            if (!$CLIservice) {write-error "No Remote CLI Services found on this server. Please ensure it is vSphere 4 or later."}
            
            $CLIservice | foreach {
                $serviceToStart = $_
                if ($enabled) {
                    write-progress -id 1 -activity "Modifying Remote CLI on $VMHost" -status "Starting $($_.Label)..."
                    $serviceToStart | start-vmhostservice
                    if ($onboot) {write-progress -id 1 -activity "Modifying Remote CLI on $VMHost" -status "Setting $($_.Label) to start up automatically..."
                        $serviceToStart | set-vmhostservice -policy "On"
                    }
                } else {
                    write-progress -id 1 -activity "Modifying Remote CLI on $VMHost" -status "Stopping $($_.Label)..."
                    $serviceToStart | stop-vmhostservice -confirm:$false
                    if ($onboot) {write-progress -id 1 -activity "Modifying Remote CLI on $VMHost" -status "Setting $($_.Label) to not start up with host..."
                        $serviceToStart | set-vmhostservice -policy "Off"
                    }
                }
            }
        }
    }
}
