8
0

Export-MSSQLServerObjects: Working on it...

This commit is contained in:
2026-02-25 16:33:41 +01:00
parent 6b78375a2b
commit c95f93bb83
2 changed files with 16 additions and 12 deletions

View File

@@ -38,6 +38,13 @@ ServerName = SDD-VMP04-SQL17\DD_DEVELOP01
#################################################################################################### ####################################################################################################
DatabaseName = DD_ECM DatabaseName = DD_ECM
####################################################################################################
# Set the filter for which objects should be exported. Default is "*", for all objects. #
# Example: * #
# Example: DEX_ #
####################################################################################################
ObjectFilter = *
#################################################################################################### ####################################################################################################
# Set if schema names should be written to the export files, like [dbo].[<ObjectName>]. # # Set if schema names should be written to the export files, like [dbo].[<ObjectName>]. #
# Example: true # # Example: true #

View File

@@ -58,11 +58,6 @@ Set-Variable -Scope Global -Name ModuleDefaultSourcePath -Value "P:\Sk
Set-Variable -Scope Global -Name ModuleHKLMRegistryPath -Value "HKLM:\SOFTWARE\Digital Data\Modules" Set-Variable -Scope Global -Name ModuleHKLMRegistryPath -Value "HKLM:\SOFTWARE\Digital Data\Modules"
Set-Variable -Scope Global -Name ModuleHKCURegistryPath -Value "HKCU:\SOFTWARE\Digital Data\Modules" Set-Variable -Scope Global -Name ModuleHKCURegistryPath -Value "HKCU:\SOFTWARE\Digital Data\Modules"
#Set-Variable -Scope Global -Name UninstallServiceProfile -Value $NULL
#Set-Variable -Scope Global -Name InstallServiceProfile -Value $NULL
#-----------------------------------------------------------------------------------------------------# #-----------------------------------------------------------------------------------------------------#
############################################ set functions ############################################ ############################################ set functions ############################################
#-----------------------------------------------------------------------------------------------------# #-----------------------------------------------------------------------------------------------------#
@@ -692,7 +687,7 @@ function Export-Objects($objects, $subfolder, $ExportWithDBSchemaName) {
if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null } if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null }
foreach ($object in $objects) { foreach ($object in $objects) {
if (-not $object.IsSystemObject) { if ((-not $object.IsSystemObject) -and ($object -match $ObjectFilter.Replace('*','.'))) {
IF ($ExportWithDBSchemaName) { IF ($ExportWithDBSchemaName) {
$fileName = "$($object.Schema).$($object.Name).sql" $fileName = "$($object.Schema).$($object.Name).sql"
@@ -714,7 +709,7 @@ function Export-Tables($db, $subfolder, $ExportWithDBSchemaName) {
if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null } if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null }
foreach ($table in $db.Tables) { foreach ($table in $db.Tables) {
if (-not $table.IsSystemObject) { if ((-not $table.IsSystemObject) -and ($table -match $ObjectFilter.Replace('*','.'))) {
IF ($ExportWithDBSchemaName) { IF ($ExportWithDBSchemaName) {
$fileName = "$($table.Schema).$($table.Name).sql" $fileName = "$($table.Schema).$($table.Name).sql"
@@ -744,13 +739,13 @@ function Export-DatabaseTriggers($db, $subfolder, $ExportWithDBSchemaName) {
$folder = Join-Path $outputFolder "$subfolder\Database" $folder = Join-Path $outputFolder "$subfolder\Database"
if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null } if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null }
foreach ($trig in $db.Triggers) { foreach ($trigger in $db.Triggers) {
if (-not $trig.IsSystemObject) { if ((-not $trigger.IsSystemObject) -and ($table -match $ObjectFilter.Replace('*','.'))) {
$fileName = "DBTrigger.$($trig.Name).sql" $fileName = "DBTrigger.$($trigger.Name).sql"
$fileName = Get-SafeFileName -InputString $fileName $fileName = Get-SafeFileName -InputString $fileName
$filePath = Join-Path $folder $fileName $filePath = Join-Path $folder $fileName
$scripter.Options.FileName = $filePath $scripter.Options.FileName = $filePath
$scripter.Script($trig) $scripter.Script($trigger)
Write-Logfile -LogLine "Exporting DB-Trigger: $filePath" Write-Logfile -LogLine "Exporting DB-Trigger: $filePath"
} }
} }
@@ -762,7 +757,7 @@ function Export-TableTriggers($db, $subfolder, $ExportWithDBSchemaName) {
if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null } if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder | Out-Null }
foreach ($table in $db.Tables) { foreach ($table in $db.Tables) {
if (-not $table.IsSystemObject) { if ((-not $table.IsSystemObject) -and ($table -match $ObjectFilter.Replace('*','.'))) {
foreach ($trig in $table.Triggers) { foreach ($trig in $table.Triggers) {
$folder = Join-Path $outputFolder "$subfolder\Table\$table" $folder = Join-Path $outputFolder "$subfolder\Table\$table"
if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder -ErrorAction SilentlyContinue | Out-Null } if (-not (Test-Path $folder)) { New-Item -ItemType Directory -Path $folder -ErrorAction SilentlyContinue | Out-Null }
@@ -836,6 +831,7 @@ FOREACH ($Module in $Modules) {
[STRING]$ServerName = $ConfigFileContent."ServerName_$($ConfigFileContent.ServerName[0])" [STRING]$ServerName = $ConfigFileContent."ServerName_$($ConfigFileContent.ServerName[0])"
[STRING]$DatabaseName = $ConfigFileContent."DatabaseName_$($ConfigFileContent.DatabaseName[0])" [STRING]$DatabaseName = $ConfigFileContent."DatabaseName_$($ConfigFileContent.DatabaseName[0])"
[STRING]$ObjectFilter = $ConfigFileContent."ObjectFilter_$($ConfigFileContent.ObjectFilter[0])"
[BOOL]$ExportWithDBSchemaName = [System.Convert]::ToBoolean(($ConfigFileContent."ExportWithDBSchemaName_1")[0]) [BOOL]$ExportWithDBSchemaName = [System.Convert]::ToBoolean(($ConfigFileContent."ExportWithDBSchemaName_1")[0])
[BOOL]$ExportStoredProcedures = [System.Convert]::ToBoolean(($ConfigFileContent."ExportStoredProcedures_1")[0]) [BOOL]$ExportStoredProcedures = [System.Convert]::ToBoolean(($ConfigFileContent."ExportStoredProcedures_1")[0])
@@ -957,6 +953,7 @@ Remove-Variable -Name ModuleHKCURegistryPath -Force -ErrorAction Sile
Remove-Variable -Name serverName -Force -ErrorAction SilentlyContinue Remove-Variable -Name serverName -Force -ErrorAction SilentlyContinue
Remove-Variable -Name databaseName -Force -ErrorAction SilentlyContinue Remove-Variable -Name databaseName -Force -ErrorAction SilentlyContinue
Remove-Variable -Name ObjectFilter -Force -ErrorAction SilentlyContinue
Remove-Variable -Name outputFolder -Force -ErrorAction SilentlyContinue Remove-Variable -Name outputFolder -Force -ErrorAction SilentlyContinue
Remove-Variable -Name ExportWithDBSchemaName -Force -ErrorAction SilentlyContinue Remove-Variable -Name ExportWithDBSchemaName -Force -ErrorAction SilentlyContinue
Remove-Variable -Name ExportStoredProcedures -Force -ErrorAction SilentlyContinue Remove-Variable -Name ExportStoredProcedures -Force -ErrorAction SilentlyContinue