Piping results from a function into a variable

Hi guys,

I have what I would have thought to be a simple issue but is turning into a bit of a headache to say the least.

Basically I am attempting to run a function which uses POSH-SSH to run a series of commands against an ESXi host.

The code seems to fail when I attempt to add the results from the function into a variable, please see below:


if ((Get-Module -Name Posh-SSH -ErrorAction SilentlyContinue) -eq $null ) { Import-Module “Posh-SSH” -Force:$true -WarningAction SilentlyContinue}

$Hosts = Get-Content Hosts.txt

$GetQLogic = “cd /proc/scsi/qla2*;head -2 1;head -2 2”

$GetEmulex = “cd /proc/scsi/lpfc*;head -5 1;head -5 2”

$FullListQLogic = @()

$FullListEmulex = @()

$Cred = get-credential

foreach ($Node in $Hosts)


$FullListQLogic += Get-SSHResult($Cred,$Node,$GetQLogic)

$FullListEmulex += Get-SSHResult($Cred,$Node,$GetEmulex)






Function Get-SSHResult() {

Param (





#Start SSH Services

start-VMHostService -HostService (Get-VMHost $ESXihost| Get-VMHostService | Where { $_.Key -eq “TSM-SSH”} ) -Confirm:$false

$Session = New-SSHSession -ComputerName $($ESXiHost) -Credential $($connectionCredentials) -AcceptKey -ConnectionTimeout 90 -KeepAliveInterval 5

$returned = Invoke-SSHCommand -Command $($sshCommand) -SessionId $Session.SessionId

if ($Session.SessionId) { $closed = Remove-SSHSession -SessionId $Session.SessionId }

#Stop ssh Services

Stop-VMHostService -HostService (Get-VMHost $ESXihost | Get-VMHostService | Where { $_.Key -eq “TSM-SSH”} ) -Confirm:$false

return ($returned)


#Connect to the vcenter first so we can enable ssh and disable ssh

Connect-VISserver “vcenter01”

#Get a set of credentials to connect with using posh-ssh

$credentials = Get-credential -Message “Enter Credentials to connect to host” -UserName “root”

$retunedSSH = Get-SSHResult -connectionCredentials $credentials -EsxiHost esx1.local.com -sshCommand “ls -al”

Write-Host $($retunedSSH.output)



It is lines 3 and 4 where I am hoping for the results to be added to fail with the following error:

Get-SSHResult : The term ‘Get-SSHResult’ is not recognized as the name of a cmdlet, function, script file, or operable program

Does anyone know the correct way in which I should be sending the resulting data into my variables?

Many thanks in advance.

Apologies, it is lines 20 and 22 which are causign the issue, thanks.

Take out the exit.


The exit is to basically stop the loop after the first attempt as otherwise I get a screen full of blood, If I remove the exit (The other exit statement I forgot to remove).

If I remove the exit it still fails with the same error.

If above shown is the script you are executing, then you need the function to be defined before it is consumed.

Ah yes of course, many thanks!

Also, don’t use exit to break out of loops. Use break.