Created
January 29, 2016 18:26
-
-
Save mattvanstone/7bd2be6385d2efcf1ce2 to your computer and use it in GitHub Desktop.
Revisions
-
mattvanstone revised this gist
Jan 29, 2016 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -55,4 +55,4 @@ Function Set-SSH { } } } } -
mattvanstone created this gist
Jan 29, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 } } } } } }