1

I am new to scripting, and I would like to ask you help in the following:

This script should be scheduled task, which is working with Veritas NetBackup, and it creates a backup register in CSV format.

I am generating two source files (.csv comma delimited):

  • One file contains: JobID, FinishDate, Policy, etc...
  • The second file contains: JobID, TapeID

It is possible that in the second file there are multiple same JobIDs with different TapeID-s.

I would like to reach that, the script for each line in source file 1 should check all of the source file 2 and if there is a JobID match, if yes, it should have the following output:

JobID,FinishDate,Policy,etc...,TapeID,TapeID....

I have tried it with the following logic, but sometimes I have no TapeID, or I have two same TapeID-s:

Contents of sourcefile 1 is in $BackupStatus

Contents of sourcefile 2 is in $TapesUsed

$FinalReport =
foreach ($FinalPart1 in $BackupStatus) {
    write-output $FinalPart1
    $MediaID =
    foreach ($line in $TapesUsed){
        write-output $line.split(",")[1] | where-object{$line.split(",")[0] -like $FinalPart1.split(",")[0]}
    }
    write-output $MediaID
}

1 Answer 1

1

If the CSV files are not huge, it is easier to use Import-Csv instead of splitting the files by hand:

$BackupStatus = Import-Csv "Sourcefile1.csv"
$TapesUsed = Import-Csv "Sourcefile2.csv"

This will generate a list of objects for each file. You can then compare these lists quite easily:

Foreach ($Entry in $BackupStatus) {
    $Match = $TapesUsed | Where {$_.JobID -eq $Entry.JobID}
    if ($Match) {
        $Output = New-Object -TypeName PSCustomObject -Property @{"JobID" = $Entry.JobID ; [...] ; "TapeID" = $Match.TapeID # replace [...] with the properties you want to use
        Export-Csv -InputObject $Output -Path <OUTPUTFILE.CSV> -Append -NoTypeInformation        }

}

This is a relatively verbose variant, but I prefer it like this. I am checking for each entry in the first file whether there is a matching entry in the second. If there is one I combine the required fields from the entry of the first list with the ones from the entry in the second list into one object that I can then export very comfortably using Export-Csv.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You! Your help is appreciated! :)

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.