I'm writing a PS script to back up a database and copy the .bak over to a different server. However, the backup seems to be happening asynchronously. And sometimes it reaches the copy command pre-maturely.
I've tried these suggestions here but it didn't quite work.
sqlbackup.Wait()
documentation here... seems to do exactly what I'm looking to do (at least on paper) but it doesn't seem to work. It still reaches my copy command before the file gets generated. Any idea how to get this to work... here is what I'm doing.
$smoBackup = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Backup
$smoBackup.Action = "Database"
$smoBackup.BackupSetDescription = "Full Backup of " + $dbName
$smoBackup.BackupSetName = $dbName + " Backup"
$smoBackup.Database = $dbName
$smoBackup.MediaDescription = "Disk"
$smoBackup.Devices.AddDevice($sourcePath, "File")
$smoBackup.CopyOnly = "true"
$smoBackup.SqlBackup($SourceSrv)
Copy-Item -Path $sourcePathUNC -Destination $destinationPathUNC