I am making a script to backup databases in SSMS from Powershell. I want to add a progress bar since these databases take some time to backup. This is what I have. The progress bar is popping up but just stays at zero percent. The databases to successfully back up as well. I think I am close but missing a few things in the script.
$SQLServer = "ServerA"
$Database = 'A', 'B', 'C', 'D', 'E', 'F', 'G'
$Databases = $Database
$TotalItems = $Databases.Count
$CurrentItem = 0
$PercentComplete = 0
Foreach ($item in $Databases)
{
Write-Progress -Activity "Starting Backups" -Status "$PercentComplete% Complete:" -PercentComplete $PercentComplete
$Name = $item
Get-SqlDatabase -Name $Name
$CurrentItem++
$PercentComplete = [int](($CurrentItem / $TotalItems) * 100)
Start-Sleep -Milliseconds 2500
}
Invoke-Sqlcmd -ServerInstance $SQLServer -Database $Database -InputFile "C:\Users\Documents\Scripts\DatabaseBackups.txt"
[int]($CurrentItem * 100.0 / $TotalItems)AlsoInvoke-SqlCmditself has no way of tracking progress, so if you were exepecting that then that won't work. I suggest you use proper software for this: SqlBackupMaster is free and works well.Backup-DbaDatabase -SqlInstance $SQLServer -Database $Database -Path 'YourBackupPath' -Type Full