Hi
Editing the previous reply.
I was able to fix my issues. Please find the updated code below. In order to export each command's output to csv, had to repeat the code.
Is there a better way, I can pass all the commands in singe line (when I plan to enable $cmdsub block) but still be able to generate one csv with output of each command to separate columns, including the columns for vCenter/Cluster/Hostname.
Import-Module VMware.VimAutomation.Core
Import-Module PoSH-SSH
$vcenter = Read-Host " Enter FQDN Or IP address of the vcenter"
$UserName = Read-Host "Enter vCenter Username"
$VCCred = Get-Credential -UserName $UserName -Message "Please enter Password of SSO Credentials"
$VCConnection = Connect-VIServer -Server $vcenter -Credential $VCCred | Out-Null
$Date = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
$ResultFile = "C:\temp\Output-$vcenter-$Date.csv"
$VMSAServiceStatus = @()
<#$cmdsub = @'
/etc/init.d/slpd stop;
/etc/init.d/slpd status;
/etc/init.d/sfcbd-watchdog stop;
/etc/init.d/sfcbd-watchdog status;
chkconfig slpd off;
chkconfig --list | grep slpd;
chkconfig sfcbd-watchdog off;
chkconfig --list | grep sfcbd-watchdog;
'@#>
$cmd1 = '/etc/init.d/slpd status'
$cmd2 = '/etc/init.d/sfcbd-watchdog status'
$secPswd = ConvertTo-SecureString 'xxxxxxx' -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ('root', $secPswd)
Get-Cluster -Name "xxxxxxx" | Get-VMHost -PipelineVariable esx |
ForEach-Object -Process {
$ServiceStatus = @()
$ServiceStatus = new-object PSObject
$ServiceStatus | add-member -type NoteProperty -Name HostName -Value $esx.Name
Get-VMHostService -VMHost $esx | where { $_.Key -eq 'TSM-SSH' } | Start-VMHostService -Confirm:$false | Out-Null
$session = New-SSHSession -ComputerName $esx.Name -Credential $cred –AcceptKey
Write-Host "$esx.Name status:"
$slpdServiceState = Invoke-SSHCommand -SSHSession $session -Command $cmd1 | select -ExpandProperty Output
$ServiceStatus | add-member -type NoteProperty -Name slpd_CurrentStatus -Value $slpdServiceState
$sfcbdServiceState = Invoke-SSHCommand -SSHSession $session -Command $cmd2 | select -ExpandProperty Output
$ServiceStatus | add-member -type NoteProperty -Name sfcbd_CurrentStatus -Value $sfcbdServiceState
Remove-SSHSession -SSHSession $session | Out-Null
Get-VMHostService -VMHost $esx | where { $_.Key -eq 'TSM-SSH' } | Stop-VMHostService -Confirm:$false | Out-Null
$VMSAServiceStatus += $ServiceStatus
}
$VMSAServiceStatus | export-csv -Path $ResultFile -notype
Write-Host "Please check the final result file - $ResultFile"