Jaydeep Chaudhari
Jaydeep Chaudhari

Reputation: 430

Add same line in multiple lines in variable

I have below PowerShell script to get files created from specific time:

$diff = "H:\DBBackup\server1\queryDB\Diff\"
$tlogs = "H:\DBBackup\server1\queryDB\trn\"

$compareDate = Get-Date (Get-Item $diff).LastWriteTime

$trn = Get-ChildItem -Path $tlogs | Where-Object {
    $_.LastWriteTime -gt $compareDate -and !($_.PSIsContainer)
} | Sort-Object LastWriteTime

$trn.Name

Below is the output of above script which is expected

SQLSLinuxServerDB_backup_2017_08_10_043503_8807816.trn
SQLSLinuxServerDB_backup_2017_08_10_044003_8906811.trn
SQLSLinuxServerDB_backup_2017_08_10_053903_4501615.trn
SQLSLinuxServerDB_backup_2017_08_11_032503_7408313.trn
SQLSLinuxServerDB_backup_2017_08_11_043003_7803417.trn
SQLSLinuxServerDB_backup_2017_08_11_044003_2408719.trn

Now I want use above values with path added in $tlog to run below commands in above script.

ascp -T -l 100m -m 10m -i "C:\Users\abc\.ssh\id_putty.ppk" H:\DBBackup\server1\queryDB\trn\SQLSLinuxServerDB_backup_2017_08_10_043503_8807816.trn [email protected]:/SQLSLinuxServerDB_backup_2017_08_10_043503_8807816.trn
ascp -T -l 100m -m 10m -i "C:\Users\abc\.ssh\id_putty.ppk" H:\DBBackup\server1\queryDB\trn\SQLSLinuxServerDB_backup_2017_08_10_044003_8906811.trn [email protected]:/SQLSLinuxServerDB_backup_2017_08_10_044003_8906811.trn
ascp -T -l 100m -m 10m -i "C:\Users\abc\.ssh\id_putty.ppk" H:\DBBackup\server1\queryDB\trn\SQLSLinuxServerDB_backup_2017_08_10_053903_4501615.trn [email protected]:/SQLSLinuxServerDB_backup_2017_08_10_053903_4501615.trn
ascp -T -l 100m -m 10m -i "C:\Users\abc\.ssh\id_putty.ppk" H:\DBBackup\server1\queryDB\trn\SQLSLinuxServerDB_backup_2017_08_11_032503_7408313.trn [email protected]:/SQLSLinuxServerDB_backup_2017_08_11_032503_7408313.trn
ascp -T -l 100m -m 10m -i "C:\Users\abc\.ssh\id_putty.ppk" H:\DBBackup\server1\queryDB\trn\SQLSLinuxServerDB_backup_2017_08_11_043003_7803417.trn [email protected]:/SQLSLinuxServerDB_backup_2017_08_11_043003_7803417.trn
ascp -T -l 100m -m 10m -i "C:\Users\abc\.ssh\id_putty.ppk" H:\DBBackup\server1\queryDB\trn\SQLSLinuxServerDB_backup_2017_08_11_044003_2408719.trn [email protected]:/SQLSLinuxServerDB_backup_2017_08_11_044003_2408719.trn

Is there any way to use above commands using string or variable in my PowerShell script instead of hardcoading it.

I tried with this

$LatestDiffFile = $tlogs + $trn.Name
$LatestDiffFile

But this is not working the way I thought.

Upvotes: 0

Views: 797

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174920

To get the latest, after sorting on LastWriteTime, grab the last index (-1) in the $trn array:

$LatestDiffFile = $tlogs + $trn[-1].Name

If you want to prepend $tlogs to all the names in $trn and issue the ascp command, use a loop:

foreach($file in $trn){
    # Construct local file path
    $LocalPath = Join-Path $tlogs $file.Name
    # Construct remote file path
    $RemotePath = '[email protected]:/{0}' -f $file.Name
    # Prepare ascp arguments
    $ascpArgs = '-T -l 100m -m 10m -i "C:\Users\abc\.ssh\id_putty.ppk"',$LocalPath,$RemotePath

    # Execute
    & ascp $ascpArgs
}

Upvotes: 1

Related Questions