PoshCode Archive  Artifact Content

Artifact fc55e134ef0f2ff627038d36cafdb525a705acfa6de9cbfd9b6daa6f3db6a549:

  • File Set-ESXRemoteCLI.ps1 — part of check-in [fded32dac5] at 2018-06-10 13:23:26 on branch trunk — 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. (user: jgrote size: 2200)

# 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"
                    }
                }
            }
        }
    }
}