Eu finalmente encontrei uma maneira limpa, brincando com as propriedades dos objetos com. Especialmente, o snippet a seguir pode testar se o arquivo está presente no arquivo zip:
foreach($file in $input)
{
$zipPackage.CopyHere($file.FullName)
$size = $zipPackage.Items().Item($file.Name).Size
while($zipPackage.Items().Item($file.Name) -Eq $null)
{
start-sleep -seconds 1
write-host "." -nonewline
}
}
O script completo é o seguinte:
$VerbosePreferenceBak = $VerbosePreference
$VerbosePreference = "Continue"
add-PSSnapin SqlServerCmdletSnapin100
function BackupDB([string] $dbName, [string] $outDir) {
Write-Host "Backup de la base : $dbName"
$script = "BACKUP DATABASE $dbName TO DISK = '$outDir\$dbName.bak' WITH FORMAT, COPY_ONLY;"
Invoke-Sqlcmd -Query "$script" -ServerInstance "." -QueryTimeOut 600
Write-Host "Ok !"
}
function Compress-ToZip {
param([string]$zipfilename)
Write-Host "Compression du dossier"
if(-not (test-path($zipfilename))) {
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(Get-ChildItem $zipfilename).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input) {
$zipPackage.CopyHere($file.FullName)
$size = $zipPackage.Items().Item($file.Name).Size
while($zipPackage.Items().Item($file.Name) -Eq $null)
{
start-sleep -seconds 1
write-host "." -nonewline
}
write-host "."
}
}
$targetDir = "E:\Backup SQL"
$date = Get-Date -format "yyyy-MM-dd"
$newDir = New-Item -ItemType Directory "$targetDir\$date\sql" -Force
BackupDB "DB1" "$newDir"
BackupDB "DB2" "$newDir"
BackupDB "DB3" "$newDir"
BackupDB "DB4" "$newDir"
Get-ChildItem "$newDir" | Compress-ToZip "$targetDir\$date\sql_$date.zip"
remove-item $newDir -Force -Confirm:$false -Recurse
$VerbosePreference = $VerbosePreferenceBak