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
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
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
Thank you very much...that was perfect 🙂