aws cli(在powershell上)在执行下一个命令之前等待cp完成

时间:2019-02-22 09:32:29

标签: powershell amazon-s3 aws-cli aws-powershell

在脚本转到下一个命令之前,如何检查复制是否成功?

    $src_dir_local = 'D:\temp\tobemoved\'
$dst_dir_local = 'D:\temp\moved\'
$log_dir = 'D:\temp\log\'
$log_file = 'backup.log'
$ofs_char = "`n"
$curr_date = Get-Date -UFormat '%Y-%m-%d'
$curr_date_time = Get-Date
$s3_bucket = 's3://mybucket/'
$s3_storage_class = 'STANDARD_IA'
$files = Get-ChildItem $src_dir_local

write-output $src_dir_local + $file + $curr_date
write-output $s3_command

aws s3 ls 

write-output 'looping through files....'
foreach($f in $files){
    $outputfilename = $f.Name

    $s3_move_command = 'time aws s3 cp --quiet' + ($src_dir_local + $outputfilename + ' ' + $s3_bucket + $curr_date + '/' + $outputfilename + ' --storage-class ' + $s3_storage_class)


    write-output $outputfile
    write-output 'Start process'

    Invoke-Expression $s3_move_command

    move-item -path ($src_dir_local + $outputfilename) -destination ($dst_dir_local + '_mvd_' + $outputfilename) -whatif

    write-output 'End process'
    write-output 'Start moving process'

    $log_message = ($ofs_char + 'File moved ' + $outputfile + ' ' + $((Get-Date).ToString()))
    if(Test-Path ($log_dir + $log_file)){
        Add-Content ($log_dir + $log_file)  $log_message
    }else{
        New-Item -path $log_dir -name $log_file -type 'file' -value 'Initial log '
        Add-Content ($log_dir + $log_file) $log_message
    }
}

1 个答案:

答案 0 :(得分:1)

通常,您会写:

aws s3 cp $src $dst

If ($lastexitcode -eq 0) {
   ...
} Else {
   ...
}

请参阅文档here

如果必须使用Invoke-Expression,请参阅linked堆栈溢出答案。