VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

How can I get output only with specific column in CSV

Hi,

I have a csv file where multiple columns are present, I would like to get the csv output using VM Name and results only.

from the below script, I am getting the output appended to existing CSV, But I need VMName and results, please help!

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | 
ForEach-Object -Process {
try
{
$session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop
$result = $session | Select-Object -ExpandProperty Connected
$output = $((Invoke-SSHCommand -SSHSession $session -Command 'uptime -s').output)
Get-SSHSession | Remove-SSHSession | Out-Null
}
catch
{
$result = 'False'
}
$row | Add-Member -MemberType NoteProperty -Name 'SSH Login' -Value $result -PassThru | Add-Member -MemberType NoteProperty -Name 'Uptime' -Value $output -PassThru
} | Export-Csv -Path $reportlocation2 -NoTypeInformation -UseCulture

Labels (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can create a new object with only the properties you want

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | 
    ForEach-Object -Process {
        try {
            $session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop
            $result = $session | Select-Object -ExpandProperty Connected
            $output = $((Invoke-SSHCommand -SSHSession $session -Command 'uptime -s').output)
            Get-SSHSession | Remove-SSHSession | Out-Null
        } catch {
            $result = 'False'
            $output = 'na'
        }
        New-Object -TypeName PSObject -Property ([ordered]@{
            Name = $row.Name
            'SSH Login' = $result
            Uptime = $output
        })
    } | Export-Csv -Path $reportlocation2 -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You can create a new object with only the properties you want

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | 
    ForEach-Object -Process {
        try {
            $session = New-SSHSession $row.Name -Credential $Cred -AcceptKey -ErrorAction Stop
            $result = $session | Select-Object -ExpandProperty Connected
            $output = $((Invoke-SSHCommand -SSHSession $session -Command 'uptime -s').output)
            Get-SSHSession | Remove-SSHSession | Out-Null
        } catch {
            $result = 'False'
            $output = 'na'
        }
        New-Object -TypeName PSObject -Property ([ordered]@{
            Name = $row.Name
            'SSH Login' = $result
            Uptime = $output
        })
    } | Export-Csv -Path $reportlocation2 -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much...that was perfect 🙂

Reply
0 Kudos