# encoding: ascii
# api: powershell
# title: Invoke-SSH Wrapper
# description: This function is a wrapper for the Invoke-SSH cmdlet which is included in the Netcmdlets package (http://www.nsoftware.com/PowerShell/). I don’t care for its output or syntax complexity, so I made this to suit my needs.
# version: 0.1
# type: function
# license: CC0
# function: Invoke-SSH
# x-poshcode-id: 646
# x-archived: 2009-04-22T16:33:49
#
# Usage:
# Invoke-SSH <server> <PSCredential> [$true|$false] <command>
# Example:
# PS > Invoke-SSH myserver $root ‘ls /’ | select -first 3
# root@myserver # ls /
# bin
# boot
# dev
#
function Invoke-SSH {
param (
[string]$Server = "$(throw 'Server is a mandatory parameter.')",
$Credential = "$(throw 'Credential is a mandatory parameter.')",
[switch]$Echo = $true,
[string]$Command = "$(throw 'Command is a mandatory parameter.')"
)
# Since function name is same as cmdlet, we have to fully-qualify cmdlet name using format Snapin\Cmdlet
$ShellObject = Netcmdlets\Invoke-SSH -Server $Server -Credential $Credential -Force -Command $Command
# Foreach loop creates a string array from the output, one line per array item
$Output = $ShellObject | ForEach-Object { ($_.Text).split("`n") }
# By default will simulate a bash-like prompt with username and server. Good for pasting in emails.
if ( $Echo ) {
$UserName = $Credential.UserName -replace "\\"
Write-Host -foregroundcolor Yellow "$UserName@$Server # $Command"
}
Write-Output $Output
}