VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

System.String[] Date field while exporting to csv

Hi,

I am unable to get the date information while exporting to csv file.

Please help!!!

Output shows as

System.String[]

Script

$reportlocation2 = '.\vm.csv'

$code = @'

`$Today = (Get-Date)

`$StartDate = `$Today.Date.AddDays(-22)

`$EndDate = `$Today.Date.AddDays(1)

"### Output ###"

(gwmi win32_operatingsystem | %{ $_.ConvertToDateTime($_.LastBootUpTime) })

'@

$report1 = @()

Import-Csv -Path $reportlocation2 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $ExecutionContext.InvokeCommand.ExpandString($code)

    }

    $result = Invoke-VMScript @sInvoke

    $dummy, $out1 = $result.ScriptOutput -split '### Output ###'

    $out1 = $out1.TrimStart("`n`r") -split "`n`r"

    $report1 += $row | Add-Member -MemberType NoteProperty -Name 'Last Reboot' -Value ([DateTime]::$out1) -PassThru

}

$report1 | Export-Csv -Path $reportlocation2 -UseCulture -NoTypeInformation

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You forgot some back-ticks in the foreach loop.
And the DateTime conversion was incorrect.

$reportlocation2 = '.\vm.csv'

$code = @'

`$Today = (Get-Date)

`$StartDate = `$Today.Date.AddDays(-22)

`$EndDate = `$Today.Date.AddDays(1)

"### Output ###"

(Get-WmiObject -Class  win32_operatingsystem | %{ `$_.ConvertToDateTime(`$_.LastBootUpTime) })

'@


$report1 = @()

Import-Csv -Path $reportlocation2 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $ExecutionContext.InvokeCommand.ExpandString($code)

    }

    $result = Invoke-VMScript @sInvoke

    $dummy, $out1 = $result.ScriptOutput -split '### Output ###'

    $out1 = $out1.TrimStart("`n`r") -split "`n`r"

    $report1 += $row | Add-Member -MemberType NoteProperty -Name 'Last Reboot' -Value ([DateTime]::Parse($out1)) -PassThru

}

$report1 | Export-Csv -Path $reportlocation2 -UseCulture -NoTypeInformation


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

You forgot some back-ticks in the foreach loop.
And the DateTime conversion was incorrect.

$reportlocation2 = '.\vm.csv'

$code = @'

`$Today = (Get-Date)

`$StartDate = `$Today.Date.AddDays(-22)

`$EndDate = `$Today.Date.AddDays(1)

"### Output ###"

(Get-WmiObject -Class  win32_operatingsystem | %{ `$_.ConvertToDateTime(`$_.LastBootUpTime) })

'@


$report1 = @()

Import-Csv -Path $reportlocation2 -UseCulture -PipelineVariable row |

ForEach-Object -Process {

    $sInvoke = @{

        VM              = $_.Name

        GuestCredential = $creds

        ScriptTYpe      = 'powershell'

        ScriptText      = $ExecutionContext.InvokeCommand.ExpandString($code)

    }

    $result = Invoke-VMScript @sInvoke

    $dummy, $out1 = $result.ScriptOutput -split '### Output ###'

    $out1 = $out1.TrimStart("`n`r") -split "`n`r"

    $report1 += $row | Add-Member -MemberType NoteProperty -Name 'Last Reboot' -Value ([DateTime]::Parse($out1)) -PassThru

}

$report1 | Export-Csv -Path $reportlocation2 -UseCulture -NoTypeInformation


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very very much. That worked perfectly Smiley Happy

0 Kudos