L
L
lohmag2016-10-24 14:47:32
PowerShell
lohmag, 2016-10-24 14:47:32

How to restore a busy database from Powershell?

I want to restore the database from a backup, everything works if no one is connected to it. If there are connections to it, then it says: "Exclusive access could not be obtained because the database is in use." How to cut off connections from powershell?

Restore-SqlDatabase -ServerInstance 'srv-sql7' -Database 'Billing'  -ReplaceDatabase -BackupFile $billingBackup.FullName;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri Goncharuk, 2016-10-25
@yukon39

$ServerName = 'srv-sql7'
$DBName = 'Billing'

$Server = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Server -ArgumentList $ServerName
$Server.KillAllProcesses($DBName)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question