help on export fields in CSV

Hello Rob

I just need two very little changes :wink:

  1. When the line are copied in the CSV, all the fields are separated with " tags. Is it possible to avoid it please?

  2. When the append of the new 13 lines occurs I just want that these 13 lines crushed the previous existing 13 lines. So i want always 13 lines in my CSV file and not more

Could you help me and after it will be perfect :wink:

For 1)
Actually not. It’s part of the csv format definition and it identifies text. All tools or applications able to import or export csv files should be able to deal with it.

For 2)
You’ve got several code examples only one of it uses the parameter -Append. If you don’t like to append the new data don’t use it. You can use the parameter -Force to overwrite the exisiting file.

Thanks Olaf

  1. I was afraid that " syntax may cause issue and make the CSV file not recognized as a CSV file… For information I use the data of the CSV file with a BI tool

In the last script of Rob below there is not an append parameter. So I dont now how to force the overwriting??

PowerShell.exe -ExecutionPolicy Bypass
$csvPath = ‘C:\Program Files\Splunk\etc\apps\Splunk_TA_windows\lookups\NZDL.csv’
$csv = Import-Csv -Path $csvPath

#Take the first 13 rows and update Ping and TimeStamp
$newContent = $csv |
Select -First 13 -Property Host,
@{Name=‘Ping’;Expression={Test-Connection -ComputerName $_.host -Quiet -Count 1}},
@{Name=‘TimeStamp’;Expression={Get-Date}},
Building,
Floor,
Gate,
Localisation

$newCSV = @() #Create new array
$newCSV += $csv #Append previous csv
$newCSV += $newContent #Append new rows

#Overwrite the existing CSV with the new CSV
$newCSV |
Export-CSV -Path $csvPath -NoTypeInformation

Putting below line inside a script will not be in effect.

PowerShell.exe -ExecutionPolicy Bypass

This is used when you have to call a script using PowerShell.exe by bypassing the current execution policy.

PowerShell.exe -ExecutionPolicy Bypass -File c:\somescript.ps1

and I would request you to use code posting tags(How to format code in this forum) to post code as it makes others easy to read and understand the code.

and for point 2: you can use -Force parameter of Export-CSv. you can do a Get-Help Export-CSV -Full for the documentation.

I have to echo what kvprasoon said … please please please use the code posting tags. You don’t even have to type them, you can click on them, they are named “pre” and you can find them right above the text you enter in the post editor.

I don’t know what’s your native language but in the code you posted you have twice an “append” in your comments!! If you don’t want to append the old data don’t do it

$csvPath = 'C:\Program Files\Splunk\etc\apps\Splunk_TA_windows\lookups\NZDL.csv'
$csv = Import-Csv -Path $csvPath | 
    Select-Object -Property Host, Building, Floor, Gate, Localisation,
        @{Name='Ping';Expression={Test-Connection -ComputerName $_.host -Quiet -Count 1}},
        @{Name='TimeStamp';Expression={Get-Date}}
$csv |
    Export-CSV -Path $csvPath -NoTypeInformation -Force

I would like to recommend for you to urgently start to learn the very basics of Powershell. What you’re asking here are beginner questions. You would save yourself from a lot of wasted time and frustrations.

Hi

Sorry, I will use code posting tags in the future

Concerning the code, it doesn’t works because there is no changes in the CSV file…

The file is available here :

https://cjoint.com/c/HHCsszTXcyf

Only the fields Timestamp and ping change every time the script runs and everytime they change I want to overwrite the old data for these fields…

Is this code is OK for doing what I want??

$csvPath = 'C:\Program Files\Splunk\etc\apps\Splunk_TA_windows\lookups\NZDL.csv'
$csv = Import-Csv -Path $csvPath

#Take the first 13 rows and update Ping and TimeStamp
$newContent = $csv |
Select -First 13 -Property Host,
@{Name='Ping';Expression={Test-Connection -ComputerName $_.host -Quiet -Count 1}},
@{Name='TimeStamp';Expression={Get-Date}},
Building,
Floor,
Gate,
Localisation

$newCSV = @() #Create new array
$newCSV += $csv #Append previous csv
$newCSV += $newContent #Append new rows

#Overwrite the existing CSV with the new CSV
$newCSV |
Export-CSV -Path $csvPath -NoTypeInformation -Force

Am I wrong or are you posting the same code again and again? :wink: Does the code do what you want? I have updated my code suggestion in my last post.
Please take your time and start to learn the basics of Powershell from scratch. You wull learn how to debug your scripts as well. Run your code line by line in the ise and check what’s in the variables you use.

No its not the same code, I added -Force parameter

But it doesnt do what I want

I doesnt understand why your code do nothing

And please understand that this specific scripts is urgent for me, I have not enough time to learn Powershell actually but it will better since one month…

So I will understand it at this time…

To watch the MVA video course “Getting Started with Powershell” takes about 8 or 10 hours or so. I assume you would have time for this already.
It’s almost impossible that my code suggestion does “nothing”. No error? No messages in the console? Nothing? Did you try to debug it?

@rosse There is no benchmark for an individual about PowerShell knowledge. We all including you are learning it everyday, what @js was suggesting you is to have the very basics first. The video tutorial in Microsoft Virtual Academy(PowerShell 3.0 Jump Start) is just few hours. that will give you a lot of information.

Please check below code is worth for you or not.

$Csv = Import-Csv -Path $CsvPath
$NewData = $Csv | Foreach-Object -Process {
            $PropertyHash = @{
                Host         = $_.Host
                TimeStamp    = (Get-Date)
                Ping         = (Test-Connection -ComputerName $_.host -Quiet -Count 1)
                Building     = $_.Building
                Floor        = $_.Floor
                Gate         = $_.Gate
                Localisation = $_.Localisation
            }

            New-Object -TypeName PSObject -Property $PropertyHash
}
$NewData | Export-Csv -Path $CsvPath -Force -NoTypeInformation

It works now…

The script is launched automatically by another tool and the issue was here…

My data are well overwritted…

Thanks a lot

:wink: :smiley: