Added: remove db files too when instance not local

This commit is contained in:
Remko Dannenberg 2021-03-30 19:25:06 +02:00
Родитель f33269f56a
Коммит 5449796080
1 изменённых файлов: 33 добавлений и 25 удалений

Просмотреть файл

@ -1,15 +1,15 @@
<#
.Synopsis
Remove Business Central Database(s) from SQL Server
.Description
Remove Business Central Database(s) from SQL Server
Windows Authentication to the SQL Server is required.
.Parameter databaseServer
database Server from which you want to remove the database(s)
.Parameter databaseInstance
database Instance on the database Server from which you want to remove the database(s)
.Parameter databaseName
database name of the database(s) you want to remove
<#
.Synopsis
Remove Business Central Database(s) from SQL Server
.Description
Remove Business Central Database(s) from SQL Server
Windows Authentication to the SQL Server is required.
.Parameter databaseServer
database Server from which you want to remove the database(s)
.Parameter databaseInstance
database Instance on the database Server from which you want to remove the database(s)
.Parameter databaseName
database name of the database(s) you want to remove
#>
function Remove-BcDatabase {
Param(
@ -34,11 +34,10 @@ function Remove-BcDatabase {
else {
$op = "="
}
if ($databaseServer -eq "localhost") {
$dbFiles = Invoke-SqlCmd `
-ServerInstance $databaseserverinstance `
-Query "SELECT f.physical_name FROM sys.sysdatabases db INNER JOIN sys.master_files f ON f.database_id = db.dbid WHERE db.name $op '$DatabaseName'" | ForEach-Object { $_.physical_name }
}
$dbFiles = Invoke-SqlCmd `
-ServerInstance $databaseserverinstance `
-Query "SELECT f.physical_name FROM sys.sysdatabases db INNER JOIN sys.master_files f ON f.database_id = db.dbid WHERE db.name $op '$DatabaseName'" | ForEach-Object { $_.physical_name }
$databases = Invoke-SqlCmd `
-ServerInstance $databaseserverinstance `
@ -57,14 +56,23 @@ function Remove-BcDatabase {
-Query "DROP DATABASE [$_]"
}
if ($databaseServer -eq "localhost") {
$dbFiles | ForEach-Object {
if (Test-Path $_) { Remove-Item -Path $_ -Force }
$dirname = [System.IO.Path]::GetDirectoryName($_)
if ((Get-ChildItem -Path $dirname | Measure-Object).Count -eq 0) {
Remove-Item -Path $dirname -Force
}
$dbFiles | ForEach-Object {
if ($databaseServer -ne "localhost") {
$qualifier = $_ | Split-Path -Qualifier
$newQualifier = '\\{0}\{1}' -f $databaseServer, $qualifier.Replace(':','$').ToLower()
$path = $_.Replace($qualifier, $newQualifier)
} else {
$path = $_
}
if (Test-Path $path) { Remove-Item -Path $path -Force }
}
if ([string]::IsNullOrEmpty($path) -eq $false -and
(Test-Path ($path | Split-Path -Parent)) -eq $true -and
[string]::IsNullOrEmpty((Get-ChildItem -Path ($path | Split-Path -Parent))) -eq $true) {
Remove-Item -Path ($path | Split-Path -Parent) -Force -ErrorAction Continue
}
}
Export-ModuleMember -Function Remove-BcDatabase
Export-ModuleMember -Function Remove-BcDatabase