Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke-DbaDbDataMasking - made azure compliant, probably #5184

Merged
merged 4 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 29 additions & 32 deletions functions/Invoke-DbaDbDataMasking.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -171,35 +171,33 @@ function Invoke-DbaDbDataMasking {
}
foreach ($columntest in $tabletest.Columns) {
if ($columntest.ColumnType -in 'hierarchyid', 'geography', 'xml', 'geometry' -and $columntest.Name -notin $Column) {
Stop-Function -Message "$($columntest.ColumnType) is not supported, please remove the column $($columntest.Name) from the $($tabletest.Name) table" -Target $tables
Stop-Function -Message "$($columntest.ColumnType) is not supported, please remove the column $($columntest.Name) from the $($tabletest.Name) table" -Target $tables -Continue
}
}
}

if (Test-FunctionInterrupt) {
return
}

$dictionary = @{}

foreach ($instance in $SqlInstance) {
try {
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $SqlCredential -MinimumVersion 9
$server = Connect-SqlInstance -SqlInstance $instance -SqlCredential $sqlcredential -MinimumVersion 9
} catch {
Stop-Function -Message "Failure" -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}

if ($Database) {
$dbs = Get-DbaDatabase -SqlInstance $server -SqlCredential $SqlCredential -Database $Database
} else {
$dbs = Get-DbaDatabase -SqlInstance $server -SqlCredential $SqlCredential -Database $tables.Name
if (-not $Database) {
$Database = $tables.Name
}

$sqlconn = $server.ConnectionContext.SqlConnectionObject.PsObject.Copy()
$sqlconn.Open()

foreach ($db in $dbs) {
foreach ($dbname in $Database) {
if ($server.VersionMajor -lt 9) {
Stop-Function -Message "SQL Server version must be 2005 or greater" -Continue
}
$db = $server.Databases[$($dbName)]

$connstring = New-DbaConnectionString -SqlInstance $instance -SqlCredential $SqlCredential -Database $dbName
$sqlconn = New-Object System.Data.SqlClient.SqlConnection $connstring
$sqlconn.Open()
$stepcounter = $nullmod = 0

foreach ($tableobject in $tables.Tables) {
Expand All @@ -222,7 +220,7 @@ function Invoke-DbaDbDataMasking {
$columnString = "[" + (($dbTable.Columns | Where-Object DataType -in $supportedDataTypes | Select-Object Name -ExpandProperty Name) -join "],[") + "]"
$query = "SELECT $($columnString) FROM [$($tableobject.Schema)].[$($tableobject.Name)]"
}
$data = $server.Databases[$($db.Name)].Query($query) | ConvertTo-DbaDataTable
$data = $db.Query($query) | ConvertTo-DbaDataTable
} catch {
Stop-Function -Message "Failure retrieving the data from table $($tableobject.Name)" -Target $Database -ErrorRecord $_ -Continue
}
Expand All @@ -244,14 +242,15 @@ function Invoke-DbaDbDataMasking {
# Get the column mask info
$columnMaskInfo = $tableobject.Columns | Where-Object Name -eq $indexColumn.Name

# Generate a new value
$newValue = $faker.$($columnMaskInfo.MaskingType).$($columnMaskInfo.SubType)()
if ($columnMaskInfo) {
# Generate a new value
$newValue = $faker.$($columnMaskInfo.MaskingType).$($columnMaskInfo.SubType)()

# Check if the value is already present as a property
if (($rowValue | Get-Member -MemberType NoteProperty).Name -notcontains $indexColumn.Name) {
$rowValue | Add-Member -Name $indexColumn.Name -Type NoteProperty -Value $newValue
# Check if the value is already present as a property
if (($rowValue | Get-Member -MemberType NoteProperty).Name -notcontains $indexColumn.Name) {
$rowValue | Add-Member -Name $indexColumn.Name -Type NoteProperty -Value $newValue
}
}

}

# To be sure the values are unique, loop as long as long as needed to generate a unique value
Expand Down Expand Up @@ -287,8 +286,6 @@ function Invoke-DbaDbDataMasking {

$uniqueValueColumns = $uniqueValueColumns | Select-Object -Unique

$sqlconn.ChangeDatabase($db.Name)

$deterministicColumns = $tables.Tables.Columns | Where-Object Deterministic -eq $true
$tablecolumns = $tableobject.Columns

Expand All @@ -305,15 +302,15 @@ function Invoke-DbaDbDataMasking {
}

if (-not $tablecolumns) {
Write-Message -Level Verbose "No columns to process in $($db.Name).$($tableobject.Schema).$($tableobject.Name), moving on"
Write-Message -Level Verbose "No columns to process in $($dbName).$($tableobject.Schema).$($tableobject.Name), moving on"
continue
}

if ($Pscmdlet.ShouldProcess($instance, "Masking $($tablecolumns.Name -join ', ') in $($data.Rows.Count) rows in $($db.Name).$($tableobject.Schema).$($tableobject.Name)")) {
if ($Pscmdlet.ShouldProcess($instance, "Masking $($tablecolumns.Name -join ', ') in $($data.Rows.Count) rows in $($dbName).$($tableobject.Schema).$($tableobject.Name)")) {

$transaction = $sqlconn.BeginTransaction()
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
Write-ProgressHelper -StepNumber ($stepcounter++) -TotalSteps $tables.Tables.Count -Activity "Masking data" -Message "Updating $($data.Rows.Count) rows in $($tableobject.Schema).$($tableobject.Name) in $($db.Name) on $instance"
Write-ProgressHelper -StepNumber ($stepcounter++) -TotalSteps $tables.Tables.Count -Activity "Masking data" -Message "Updating $($data.Rows.Count) rows in $($tableobject.Schema).$($tableobject.Name) in $($dbName) on $instance"

# Loop through each of the rows and change them
$rowNumber = 0
Expand Down Expand Up @@ -633,7 +630,7 @@ function Invoke-DbaDbDataMasking {
ComputerName = $db.Parent.ComputerName
InstanceName = $db.Parent.ServiceName
SqlInstance = $db.Parent.DomainInstanceName
Database = $db.Name
Database = $dbName
Schema = $tableobject.Schema
Table = $tableobject.Name
Columns = $tableobject.Columns.Name
Expand All @@ -649,11 +646,11 @@ function Invoke-DbaDbDataMasking {
# Empty the unique values array
$uniqueValues = $null
}
}
try {
$sqlconn.Close()
} catch {
Stop-Function -Message "Failure" -Continue -ErrorRecord $_
try {
$sqlconn.Close()
} catch {
Stop-Function -Message "Failure" -Continue -ErrorRecord $_
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion functions/New-DbaDbMaskingConfig.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,9 @@ function New-DbaDbMaskingConfig {
# Write the data to the Path
if ($results) {
try {
$temppath = "$Path\$($server.Name.Replace('\', '$')).$($db.Name).tables.json"
$filenamepart = $server.Name.Replace('\', '$').Replace('TCP:', '').Replace(',', '.')
$temppath = "$Path\$($filenamepart).$($db.Name).tables.json"

if (-not $script:isWindows) {
$temppath = $temppath.Replace("\", "/")
}
Expand Down