Hello,
here we go again,
situation: I am trying to develop an Automation-script, which should remove the double quote and avoid the break line. If I open the CSV file with Notepad++.
Problem: below you see the example.
$report = [System.Collections.ArrayList]@()
Write-Host "Gathering VM statistics"
Measure-Command{
foreach($vm in Get-View -ViewType Virtualmachine) {
if (!$vm.Config) { continue }
$vms = [PSCustomObject]@{
VMName = $vm.Name
Hostname = $vm.guest.hostname
OS = $vm.Config.GuestFullName
IPAddress = $vm.guest.ipAddress
Boottime = $vm.Runtime.BootTime
#PoweredOff = ""
VMState = $vm.summary.runtime.powerState
UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)
ProvisionedSpaceGB = [math]::Round(($vm.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,2)
MemoryUsage = $vm.summary.quickStats.guestMemoryUsage
Datastore = $vm.Config.DatastoreUrl[0].Name
CreatedDate = $vm.Config.CreateDate
Notes = $vm.Config.Annotation
}
$Report.Add($vms) | Out-Null
}
}
$Report | Export-Csv -Path C:\Users\Administrator\Downloads\report_$(Get-Date -Format 'yyyy_MM_dd').csv -NoTypeInformation -Delimiter ";"
$csv = 'C:\Users\Administrator\Downloads\report_2020_06_04.csv'
(Import-Csv $csv -Header "VMName", "Hostname", "OS", "IPAddress", "Boottime", "PoweredOff", "VMState", "UsedSpaceGB", "ProvisionedSpaceGB", "MemoryUsage", "Datastore", "CreatedDate", "Unit", "To_Archive", "Responsible_Team", "Backup_Strategy", "Notes") | % {
$_.Value = $_.Value -replace "`r`n",' '
$_ } | Export-Csv $csv -NoTypeInformation -UseCulture | ConvertTo-Csv -NoTypeInformation | % { $_ -replace '"', ''} | Out-File $csv
In this script, I want to export some values in CSV first and after that, the double quotes should be removed from the CSV file and there should be no break line. but i'm stuck and don't know how to compare the date of the exported file and make changes.
another solution is:
Get-Content C:\Users\Administrator\Downloads\report_2020_04_06.csv | Foreach-Object {$_ -replace '"', ''} | Out-File C:\Users\Administrator\Downloads\report_Test.csv -Force -Encoding ascii
but again the same problem, the script runs daily with Task Scheduler with Date. (report_$(Get-Date -Format 'yyyy_MM_dd').csv).