VMware Cloud Community
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Unable to get the date output proper format in the csv

Hi,

I am trying to get the Date Information from multiple VMs, I am getting the date but when I open the output csv file, the date is not showing half info but when I open the csv file in the notepad, I am seeing the output but there is a closing (") double quote in the next line

Please help!!

$code = @'
"### Output ###"
("$(Get-Date)", "$(Get-TimeZone)") -join '::'
'@

$report = @()

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | Where {$_.OS -match 'Microsoft'} |
ForEach-Object -Process {
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
}
$result = Invoke-VMScript @sInvoke
$dummy, $out1 = $result.ScriptOutput -split '### Output ###'
$out1 = $out1.TrimStart("`n`r")
$report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($out1) -PassThru
}
$report | ft -auto

ganapa2000_0-1657813152223.png

 

ganapa2000_1-1657813259686.png

 

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I added the option to remove every CR and LF in the string


$code = @'
(Get-Date), (Get-TimeZone) -join '::'
'@

$report = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | #Where {$_.OS -match 'Microsoft'} |
ForEach-Object -Process {
  $sInvoke = @{
    VM = $_.Name
    GuestCredential = $Creds
    ScriptTYpe = 'powershell'
    ScriptText = $code
  }
  $result = Invoke-VMScript @sInvoke
  $out1 = $result.ScriptOutput.Trim("`n`r")
  $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($out1) -PassThru
}
$report | Format-Table -Wrap -auto
$report | Export-Csv -Path ".\Date_Info.csv" -UseCulture -NoTypeInformation




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

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership
Jump to solution

Why the double quotes around those cmdlets?
and the '### Output ###' line?
You could just do

$code = @'
(Get-Date), (Get-TimeZone) -join '::'
'@

$report = @()

Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | Where {$_.OS -match 'Microsoft'} |
ForEach-Object -Process {
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
}
$result = Invoke-VMScript @sInvoke
$out1 = $result.ScriptOutput
$report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($out1) -PassThru
}
$report | ft -auto




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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Hi LucD,

The closing quotes after the date is coming in next line because of which the csv is showing the date in incorrect.

ideally the closing quotes should be same line, how can I get that ?

ganapa2000_0-1657818324488.png

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you try the code I just posted?


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Yes, I tried, still the same

$code = @'
(Get-Date), (Get-TimeZone) -join '::'
'@

$report = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | #Where {$_.OS -match 'Microsoft'} |
ForEach-Object -Process {
$sInvoke = @{
VM = $_.Name
GuestCredential = $Creds
ScriptTYpe = 'powershell'
ScriptText = $code
}
$result = Invoke-VMScript @sInvoke
$out1 = $result.ScriptOutput
$report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($out1) -PassThru
}
$report | ft -Wrap -auto
$report | Export-Csv -Path ".\Date_Info.csv" -UseCulture -NoTypeInformation

0 Kudos
LucD
Leadership
Leadership
Jump to solution

I added the option to remove every CR and LF in the string


$code = @'
(Get-Date), (Get-TimeZone) -join '::'
'@

$report = @()
Import-Csv -Path $reportlocation1 -UseCulture -PipelineVariable row | #Where {$_.OS -match 'Microsoft'} |
ForEach-Object -Process {
  $sInvoke = @{
    VM = $_.Name
    GuestCredential = $Creds
    ScriptTYpe = 'powershell'
    ScriptText = $code
  }
  $result = Invoke-VMScript @sInvoke
  $out1 = $result.ScriptOutput.Trim("`n`r")
  $report += $row | Add-Member -MemberType NoteProperty -Name 'DateTime' -Value ($out1) -PassThru
}
$report | Format-Table -Wrap -auto
$report | Export-Csv -Path ".\Date_Info.csv" -UseCulture -NoTypeInformation




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

0 Kudos
ganapa2000
Hot Shot
Hot Shot
Jump to solution

Thank you very much...that worked perfectly 🙂

0 Kudos