Skip to content

Instantly share code, notes, and snippets.

@mattvanstone
Created January 29, 2016 18:26
Show Gist options
  • Save mattvanstone/7bd2be6385d2efcf1ce2 to your computer and use it in GitHub Desktop.
Save mattvanstone/7bd2be6385d2efcf1ce2 to your computer and use it in GitHub Desktop.

Revisions

  1. mattvanstone revised this gist Jan 29, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion set-ssh.ps1
    Original file line number Diff line number Diff line change
    @@ -55,4 +55,4 @@ Function Set-SSH {
    }
    }
    }
    }
    }
  2. mattvanstone created this gist Jan 29, 2016.
    58 changes: 58 additions & 0 deletions set-ssh.ps1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,58 @@
    <#
    .SYNOPSIS
    Start or stop SSH on an ESXi host
    .DESCRIPTION
    Takes a hostname or a piped Get-VMHost object starts or stops the SSH service depending on the value of the State parameter
    .PARAMETER VMHost
    The name of a host to start ssh on or a VMHost object
    .PARAMETER State
    State to set the SSH service to. Valid values are Start or Stop
    .EXAMPLE
    Start-SSH esx01.domain.com Start
    .EXAMPLE
    Start-SSH esx01.domain.com Stop
    .EXAMPLE
    Get-VMHost esx01.domain.com | Set-SSH -State Start
    .EXAMPLE
    Get-VMHost esx0* | Set-SSH -State Start
    .EXAMPLE
    $VMHost = Get-VMHost esx0*
    Set-SSH $VMHost -State Stop
    #>
    Function Set-SSH {
    [CmdletBinding()] Param(
    [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]$VMHost,
    [Parameter(Mandatory=$True)][ValidateSet("Start","Stop")][String]$State
    )

    Process {
    # Check if VMHost input is a string, if so get VMHost Object then pipe to Set-SSH again so that process block is run for each item
    if ($VMHost.GetType().Name -eq "String") {
    $VMHostObj = Get-VMHost $VMHost -ErrorAction SilentlyContinue
    if (!$VMHostObj) {
    Write-Warning "Could not get host $VMHost"
    break
    } else {
    $VMHostObj | Set-SSH -State $State
    }
    } else {
    $VMHost = $_
    $VMHost | Get-VMHostService | Where {$_.key -like "TSM-SSH"} | % {
    if ($State -eq "Start") {
    if (-not $_.running) {
    $_ | Start-VMHostService -Confirm:$false | Select VMHost,Label,Running
    } else {
    $VMHost | Get-VMHostService | Where {$_.key -like "TSM-SSH"} | Select VMHost,Label,Running
    }
    }
    if ($State -eq "Stop") {
    if ($_.running) {
    $_ | Stop-VMHostService -Confirm:$false | Select VMHost,Label,Running
    } else {
    $VMHost | Get-VMHostService | Where {$_.key -like "TSM-SSH"} | Select VMHost,Label,Running
    }
    }
    }
    }
    }
    }