Skip to content

Commit

Permalink
Export Add Append and other Fixes (#5627)
Browse files Browse the repository at this point in the history
* not done yet

test with Export-DbaInstance -SqlInstance sql2017

* lets see what fails

* pupdates

* phfewf

* fixes

* update test

* fix some things

* fixed dat

* remove time consuming test

* this is problematic

* add things

* add progress, still needs work

* lil fixes

* pupdate

* finally

* fix that

* formatting

* fixed

* finally
  • Loading branch information
potatoqualitee authored May 28, 2019
1 parent 9a23672 commit 490196c
Show file tree
Hide file tree
Showing 36 changed files with 684 additions and 849 deletions.
169 changes: 0 additions & 169 deletions functions/Export-DbaAvailabilityGroup.ps1

This file was deleted.

33 changes: 17 additions & 16 deletions functions/Export-DbaCredential.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ function Export-DbaCredential {
Login to the target OS using alternative credentials. Accepts credential objects (Get-Credential)
.PARAMETER Path
The path to the exported sql file.
The path to the directory that will contain the exported sql file.
.PARAMETER FilePath
The specific path to a file which will contain the output.
.PARAMETER Identity
The credentials to export. If unspecified, all credentials will be exported.
Expand Down Expand Up @@ -58,18 +61,23 @@ function Export-DbaCredential {
[PSCredential]$SqlCredential,
[PSCredential]$Credential,
[string]$Path = (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport'),
[Alias("OutFile", "FileName")]
[string]$FilePath,
[switch]$ExcludePassword,
[switch]$Append,
[Parameter(ValueFromPipeline)]
[Microsoft.SqlServer.Management.Smo.Credential[]]$InputObject,
[switch]$EnableException
)
begin {
$null = Test-ExportDirectory -Path $Path
$serverArray = @()
$credentialArray = @{}
$credentialCollection = New-Object System.Collections.ArrayList
}
process {
if (Test-FunctionInterrupt) { return }

if (-not $InputObject -and -not $SqlInstance) {
Stop-Function -Message "You must pipe in a Credential or specify a SqlInstance"
return
Expand Down Expand Up @@ -114,7 +122,7 @@ function Export-DbaCredential {
$creds | Add-Member -MemberType NoteProperty -Name 'ExcludePassword' -Value $ExcludePassword
$credentialCollection.Add($credObject) | Out-Null
} else {
if (!(Test-SqlSa -SqlInstance $server)) {
if (-not (Test-SqlSa -SqlInstance $server)) {
Stop-Function -Message "Not a sysadmin on $instance. Quitting." -Target $instance -Continue
}

Expand Down Expand Up @@ -147,23 +155,17 @@ function Export-DbaCredential {
$key = $input.Parent.Name + '::[' + $input.Name + ']'
$credentialArray.add( $key, $true )
}
$timenow = (Get-Date -uformat "%m%d%Y%H%M%S")
$path = Join-DbaPath -Path $Path -Child "$($server.name.replace('\', '$'))-$timenow-credential.sql"
}
}

end {
$sql = @()
foreach ($cred in $credentialCollection) {
Write-Message -Level Verbose -Message "Credentials in object = $($cred.Count)"
if (-not (Test-Bound -ParameterName Path)) {
$time = (Get-Date -Format yyyMMddHHmmss)
$mydocs = [Environment]::GetFolderPath('MyDocuments')
$serverName = $($cred[0].SqlInstance.replace('\', '$'))
$path = Join-DbaPath -Path $mydocs "$serverName-$time-credential.sql"
}

foreach ($currentCred in $creds) {
$FilePath = Get-ExportFilePath -Path $PSBoundParameters.Path -FilePath $PSBoundParameters.FilePath -ServerName $currentCred.SqlInstance -Type Sql

$key = $currentCred.SqlInstance + '::' + $currentCred.Name
if ( $credentialArray.ContainsKey($key) ) {
$name = $currentCred.Name.Replace("'", "''")
Expand All @@ -181,16 +183,15 @@ function Export-DbaCredential {

try {
if ($Append) {
Add-Content -Path $path -Value $sql
Add-Content -Path $FilePath -Value $sql
} else {
Set-Content -Path $path -Value $sql
Set-Content -Path $FilePath -Value $sql
}
} catch {
Stop-Function -Message "Can't write to $path" -ErrorRecord $_ -Continue
Stop-Function -Message "Can't write to $FilePath" -ErrorRecord $_ -Continue
}

Write-Message -Level Verbose -Message "Credentials exported to $path"
Get-ChildItem -Path $FilePath
Write-Message -Level Verbose -Message "Credentials exported to $FilePath"
}
}

}
61 changes: 27 additions & 34 deletions functions/Export-DbaDacPackage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ function Export-DbaDacPackage {
Allows you to login to servers using alternative logins instead Integrated, accepts Credential object created by Get-Credential
.PARAMETER Path
The directory where the .dacpac files will be exported to. Defaults to documents.
Specifies the directory where the file or files will be exported.
.PARAMETER FilePath
Specifies the full file path of the output file.
.PARAMETER Database
The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed.
Expand Down Expand Up @@ -94,6 +97,8 @@ function Export-DbaDacPackage {
[object[]]$ExcludeDatabase,
[switch]$AllUserDatabases,
[string]$Path = (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport'),
[Alias("OutFile", "FileName")]
[string]$FilePath,
[parameter(ParameterSetName = 'SMO')]
[Alias('ExtractOptions', 'ExportOptions', 'DacExtractOptions', 'DacExportOptions', 'Options', 'Option')]
[object]$DacOption,
Expand All @@ -107,31 +112,17 @@ function Export-DbaDacPackage {
[string[]]$Table,
[switch]$EnableException
)

begin {
$null = Test-ExportDirectory -Path $Path
}
process {
if (Test-FunctionInterrupt) { return }

if ((Test-Bound -Not -ParameterName Database) -and (Test-Bound -Not -ParameterName ExcludeDatabase) -and (Test-Bound -Not -ParameterName AllUserDatabases)) {
Stop-Function -Message "You must specify databases to execute against using either -Database, -ExcludeDatabase or -AllUserDatabases"
return
}

if (-not (Test-Path $Path)) {
Write-Message -Level Verbose "Assuming that $Path is a file path"
$parentFolder = Split-Path $path -Parent
if (-not (Test-Path $parentFolder)) {
Stop-Function -Message "$parentFolder doesn't exist or access denied"
return
}
$leaf = Split-Path $path -Leaf
$fileName = Join-Path (Get-Item $parentFolder) $leaf
} else {
$fileItem = Get-Item $Path
if ($fileItem -is [System.IO.DirectoryInfo]) {
$parentFolder = $fileItem.FullName
} elseif ($fileItem -is [System.IO.FileInfo]) {
$fileName = $fileItem.FullName
}
}

if (-not $script:core) {
$dacfxPath = Resolve-Path -Path "$script:PSModuleRoot\bin\smo\Microsoft.SqlServer.Dac.dll"

Expand Down Expand Up @@ -208,23 +199,25 @@ function Export-DbaDacPackage {
if ($connstring -notmatch 'Database=') {
$connstring = "$connstring;Database=$dbname"
}
if ($fileName) {
$currentFileName = $fileName
} else {
if ($Type -eq 'Dacpac') { $ext = 'dacpac' }
elseif ($Type -eq 'Bacpac') { $ext = 'bacpac' }
$currentFileName = Join-Path $parentFolder "$cleaninstance-$dbname.$ext"
}

Write-Message -Level Verbose -Message "Using connection string $connstring"

if ($Type -eq 'Dacpac') {
$ext = 'dacpac'
} elseif ($Type -eq 'Bacpac') {
$ext = 'bacpac'
}

$FilePath = Get-ExportFilePath -Path $PSBoundParameters.Path -FilePath $PSBoundParameters.FilePath -Type $ext -ServerName $instance

#using SMO by default
if ($PsCmdlet.ParameterSetName -eq 'SMO') {
try {
$dacSvc = New-Object -TypeName Microsoft.SqlServer.Dac.DacServices -ArgumentList $connstring -ErrorAction Stop
} catch {
Stop-Function -Message "Could not connect to the connection string $connstring" -Target $instance -Continue
}
if (!$DacOption) {
if (-not $DacOption) {
$opts = New-DbaDacOption -Type $Type -Action Export
} else {
$opts = $DacOption
Expand All @@ -233,20 +226,20 @@ function Export-DbaDacPackage {
$null = $output = Register-ObjectEvent -InputObject $dacSvc -EventName "Message" -SourceIdentifier "msg" -Action { $EventArgs.Message.Message }

if ($Type -eq 'Dacpac') {
Write-Message -Level Verbose -Message "Initiating Dacpac extract to $currentFileName"
Write-Message -Level Verbose -Message "Initiating Dacpac extract to $FilePath"
#not sure how to extract that info from the existing DAC application, leaving 1.0.0.0 for now
$version = New-Object System.Version -ArgumentList '1.0.0.0'
try {
$dacSvc.Extract($currentFileName, $dbname, $dbname, $version, $null, $tblList, $opts, $null)
$dacSvc.Extract($FilePath, $dbname, $dbname, $version, $null, $tblList, $opts, $null)
} catch {
Stop-Function -Message "DacServices extraction failure" -ErrorRecord $_ -Continue
} finally {
Unregister-Event -SourceIdentifier "msg"
}
} elseif ($Type -eq 'Bacpac') {
Write-Message -Level Verbose -Message "Initiating Bacpac export to $currentFileName"
Write-Message -Level Verbose -Message "Initiating Bacpac export to $FilePath"
try {
$dacSvc.ExportBacpac($currentFileName, $dbname, $opts, $tblList, $null)
$dacSvc.ExportBacpac($FilePath, $dbname, $opts, $tblList, $null)
} catch {
Stop-Function -Message "DacServices export failure" -ErrorRecord $_ -Continue
} finally {
Expand All @@ -259,7 +252,7 @@ function Export-DbaDacPackage {
elseif ($Type -eq 'Bacpac') { $action = 'Export' }
$cmdConnString = $connstring.Replace('"', "'")

$sqlPackageArgs = "/action:$action /tf:""$currentFileName"" /SourceConnectionString:""$cmdConnString"" $ExtendedParameters $ExtendedProperties"
$sqlPackageArgs = "/action:$action /tf:""$FilePath"" /SourceConnectionString:""$cmdConnString"" $ExtendedParameters $ExtendedProperties"

try {
$startprocess = New-Object System.Diagnostics.ProcessStartInfo
Expand Down Expand Up @@ -290,7 +283,7 @@ function Export-DbaDacPackage {
InstanceName = $server.ServiceName
SqlInstance = $server.DomainInstanceName
Database = $dbname
Path = $currentFileName
Path = $FilePath
Elapsed = [prettytimespan]($resultstime.Elapsed)
Result = $finalResult
} | Select-DefaultView -ExcludeProperty ComputerName, InstanceName
Expand Down
Loading

0 comments on commit 490196c

Please sign in to comment.