forked from dataplat/dbatools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get-DbaBinaryFileTable + bunch of updates to binary commands (datapla…
- Loading branch information
1 parent
7bed5c9
commit 8b9464a
Showing
8 changed files
with
273 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
function Get-DbaBinaryFileTable { | ||
<# | ||
.SYNOPSIS | ||
Gets a table with binary columns which can be used with Export-DbaBinaryFile and Import-DbaBinaryFile. | ||
.DESCRIPTION | ||
Gets a table with binary columns which can be used with Export-DbaBinaryFile and Import-DbaBinaryFile. | ||
.PARAMETER SqlInstance | ||
The target SQL Server instance or instances. This can be a collection and receive pipeline input. | ||
.PARAMETER SqlCredential | ||
Login to the target instance using alternative credentials. Accepts PowerShell credentials (Get-Credential). | ||
Windows Authentication, SQL Server Authentication, Active Directory - Password, and Active Directory - Integrated are all supported. | ||
For MFA support, please use Connect-DbaInstance. | ||
.PARAMETER Database | ||
The database(s) to process - this list is auto-populated from the server. If unspecified, all databases will be processed. | ||
.PARAMETER Table | ||
Define a specific table you would like to query. You can specify up to three-part name like db.sch.tbl. | ||
If the object has special characters please wrap them in square brackets [ ]. | ||
Using dbo.First.Table will try to find table named 'Table' on schema 'First' and database 'dbo'. | ||
The correct way to find table named 'First.Table' on schema 'dbo' is by passing dbo.[First.Table] | ||
Any actual usage of the ] must be escaped by duplicating the ] character. | ||
The correct way to find a table Name] in schema Schema.Name is by passing [Schema.Name].[Name]]] | ||
.PARAMETER Schema | ||
Only return tables from the specified schema | ||
.PARAMETER InputObject | ||
Table objects to be piped in from Get-DbaDbTable | ||
.PARAMETER EnableException | ||
By default, when something goes wrong we try to catch it, interpret it and give you a friendly warning message. | ||
This avoids overwhelming you with "sea of red" exceptions, but is inconvenient because it basically disables advanced scripting. | ||
Using this switch turns this "nice by default" feature off and enables you to catch exceptions with your own try/catch. | ||
.NOTES | ||
Tags: Migration, Backup, Export | ||
Author: Chrissy LeMaire (@cl), netnerds.net | ||
Website: https://dbatools.io | ||
Copyright: (c) 2022 by dbatools, licensed under MIT | ||
License: MIT https://opensource.org/licenses/MIT | ||
.LINK | ||
https://dbatools.io/Get-DbaBinaryFileTable | ||
.EXAMPLE | ||
PS C:\> Get-DbaBinaryFileTable -SqlInstance sqlcs -Database test | ||
Returns a table with binary columns which can be used with Export-DbaBinaryFile and Import-DbaBinaryFile. | ||
.EXAMPLE | ||
PS C:\> Get-DbaBinaryFileTable -SqlInstance sqlcs -Database test | Out-GridView -Passthru | Export-DbaBinaryFile -Path C:\temp | ||
Allows you to pick tables with columns to be exported by Export-DbaBinaryFile | ||
#> | ||
[CmdletBinding()] | ||
param ( | ||
[DbaInstanceParameter[]]$SqlInstance, | ||
[PSCredential]$SqlCredential, | ||
[string[]]$Database, | ||
[string[]]$Table, | ||
[string[]]$Schema, | ||
[parameter(ValueFromPipeline)] | ||
[Microsoft.SqlServer.Management.Smo.Table[]]$InputObject, | ||
[switch]$EnableException | ||
) | ||
process { | ||
if (Test-FunctionInterrupt) { return } | ||
|
||
if (-not $InputObject) { | ||
try { | ||
$InputObject = Get-DbaDbTable -SqlInstance $SqlInstance -Database $Database -Table $Table -Schema $Schema -SqlCredential $SqlCredential -EnableException | ||
} catch { | ||
Stop-Function -Message "Failed to get tables" -ErrorRecord $PSItem | ||
return | ||
} | ||
} | ||
|
||
Write-Message -Level Verbose -Message "Found $($InputObject.count) tables" | ||
foreach ($tbl in $InputObject) { | ||
$server = $tbl.Parent.Parent | ||
$BinaryColumn = ($tbl.Columns | Where-Object { $PSItem.DataType.Name -match "binary" -or $PSItem.DataType.Name -eq "image" }).Name | ||
$FileNameColumn = ($tbl.Columns | Where-Object Name -Match Name).Name | ||
if ($FileNameColumn.Count -gt 1) { | ||
Write-Message -Level Verbose -Message "Multiple column names match the phrase 'name' in $($tbl.Name) in $($tbl.Parent.Name) on $($server.Name). Please specify the column to use with -FileNameColumn" | ||
} | ||
if ($BinaryColumn.Count -gt 1) { | ||
Write-Message -Level Verbose -Message "Multiple columns have a binary datatype in $($tbl.Name) in $($tbl.Parent.Name) on $($server.Name)." | ||
} | ||
if ($BinaryColumn) { | ||
$tbl | Add-Member -NotePropertyName BinaryColumn -NotePropertyValue $BinaryColumn | ||
$tbl | Add-Member -NotePropertyName FileNameColumn -NotePropertyValue $FileNameColumn -PassThru | Select-DefaultView -Property "ComputerName", "InstanceName", "SqlInstance", "Database", "Schema", "Name", "BinaryColumn", "FileNameColumn" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.