I got this cmdlet to restore a database, and it works fine if nothing is used using an existing database.
Restore-SqlDatabase -ServerInstance $databaseServerInstance -Database $database -BackupFile $backupLocation -ReplaceDatabase
But rewriting fails when the database is used:
System.Data.SqlClient.SqlError: Exclusive access cannot be obtained because the database is in use
I could not understand if there was an easy way (via argument) to override this and kill the connections or put the database in single-user mode. Does something like this exist? or do I need to switch to SMO to do something more complex than basic recovery?
Now I am using a workaround to use the SMO API in order to reset the database first, but would like to simplify this if possible.
$srv = new-object Microsoft.SqlServer.Management.Smo.Server($databaseServerInstance)
source share