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
You forgot some back-ticks in the foreach loop.
And the DateTime conversion was incorrect.
$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
You forgot some back-ticks in the foreach loop.
And the DateTime conversion was incorrect.
$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
Thank you very very much. That worked perfectly