0

Im doing an archive process and I want to insert the results to a CSV file. The process will looks like,

$source_path="D:\backup"
$destination="E:\backup"
$start_datetime=date
copy source target
$end_datetime=date

CSV file looks like

Date | Start | end | Source | Destination

I want to insert the below data.

DATE = today's date.
Start = $start_datetime
end = $end_datetime
Source = $source_path
Destination=$destination

I tried some ways to write data into CSV, but I couldn't reach the exact thing.

Can anyone help me on this?

2 Answers 2

1

Look into psobjects - Example guide

For example:

[array]$myObject = [pscustomobject]@{
    DATE = today's date.
    Start = $start_datetime
    end = $end_datetime
    Source = $source_path
    Destination=$destination
}

$myObject | Export-Csv C:\temp\myfile.csv

Edit - I'm not sure how you'll be using Export-Csv. So I will give a couple of examples. However, this really goes beyond the scope of just exporting CSVs as asked. I would recommend rewriting it with more detail about the requirements, or breaking it into several questions.

foreach($thing in $things){

    # increment array with multiple rows
    [array]$myObject += [pscustomobject]@{
        DATE = today's date.
        Start = $start_datetime
        end = $end_datetime
        Source = $source_path
        Destination=$destination
    }
}

# add data to an existing csv, which has the heading DATEM Start, end, Source, Destination
$myObject | Export-Csv C:\temp\myfile.csv -Append

# if/else logic
if($end_time -gt $somevariable){
    [array]$myObject = [pscustomobject]@{
        DATE = today's date.
        Start = $start_datetime
        end = $end_datetime
        Source = $source_path
        Destination=$destination
    }         
}else{
    [array]$myObject = [pscustomobject]@{
        DATE = someotherdate
        Start = $start_datetime
        end = $end_datetime
        Source = $source_path
        Destination=$destination
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, But every execution, this CSV file is replaced with the new data, I want to keep upto date.
Do you mean you want to add to the csv each time the script is ran? So keep old data and add new data?
Because, the sync file sometimes < 1GB sometimes > 30GB, so based on the end_time, I can come up with which date synced more data. For this I need to track it. Like wise, one more sync scripts with different Export to CSV i'll prepare
1

You've provided few specific details and only sparse pseudocode, so it's difficult to tell exactly what you're doing or exactly what you want. However, if you just want to append a record to a CSV file, Export-Csv has an -Append switch. You can pipe a custom object to Export-Csv as you go.

[PSCustomObject]@{
    DATE = today's date.
    Start = $start_datetime
    end = $end_datetime
    Source = $source_path
    Destination=$destination
} | Export-Csv -Path $CsvFile -Append -NoTypeInformation

1 Comment

Thanks for sharing, it helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.