Skip to content

Commit

Permalink
Export-DbaLogin - few fixes (#5959)
Browse files Browse the repository at this point in the history
* add order to dbs in script - currently doesn't keep them in same order

* couple of spelling fixes

* fixes #5957

* rename param
  • Loading branch information
jpomfret authored and potatoqualitee committed Aug 13, 2019
1 parent 3a6520b commit cd917c4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
15 changes: 8 additions & 7 deletions functions/Export-DbaLogin.ps1
Expand Up @@ -27,15 +27,15 @@ function Export-DbaLogin {
.PARAMETER ExcludeJobs
If this switch is enabled, Agent job ownership will not be exported.
.PARAMETER ExcludeDatabases
.PARAMETER ExcludeDatabase
If this switch is enabled, mappings for databases will not be exported.
.PARAMETER ExcludePassword
If this switch is enabled, hashed passwords will not be exported.
.PARAMETER DefaultDatabase
If this switch is enabled, all logins will be scripted with specified default database,
that could help to successfuly import logins on server that is missing default database for login.
that could help to successfully import logins on server that is missing default database for login.
.PARAMETER Path
Specifies the directory where the file or files will be exported.
Expand Down Expand Up @@ -155,7 +155,8 @@ function Export-DbaLogin {
[object[]]$ExcludeLogin,
[object[]]$Database,
[switch]$ExcludeJobs,
[switch]$ExcludeDatabases,
[Alias("ExcludeDatabases")]
[switch]$ExcludeDatabase,
[switch]$ExcludePassword,
[string]$DefaultDatabase,
[string]$Path = (Get-DbatoolsConfigValue -FullName 'Path.DbatoolsExport'),
Expand Down Expand Up @@ -222,12 +223,12 @@ function Export-DbaLogin {
}
}

if ($ExcludeDatabases -eq $false -or $Database) {
if ($ExcludeDatabase -eq $false -or $Database) {
# if we got a database or a list of databases passed
# and we need to enumerate mappings, login.enumdatabasemappings() takes forever
# the cool thing though is that database.enumloginmappings() is fast. A lot.
# if we get a list of databases passed (or even the default list of all the databases)
# we save outself a call to enumloginmappings if there is no map at all
# we save ourself a call to enumloginmappings if there is no map at all
$DbMapping = @()
$DbsToMap = $server.Databases
if ($Database) {
Expand Down Expand Up @@ -432,8 +433,8 @@ function Export-DbaLogin {
}
}

if ($ExcludeDatabases -eq $false) {
$dbs = $sourceLogin.EnumDatabaseMappings()
if ($ExcludeDatabase -eq $false) {
$dbs = $sourceLogin.EnumDatabaseMappings() | Sort-Object DBName

if ($Database) {
if ($Database[0].GetType().FullName -eq 'Microsoft.SqlServer.Management.Smo.Database') {
Expand Down
14 changes: 7 additions & 7 deletions tests/Export-DbaLogin.Tests.ps1
Expand Up @@ -5,7 +5,7 @@ Write-Host -Object "Running $PSCommandPath" -ForegroundColor Cyan
Describe "$CommandName Unit Tests" -Tag 'UnitTests' {
Context "Validate parameters" {
[object[]]$params = (Get-Command $CommandName).Parameters.Keys | Where-Object {$_ -notin ('whatif', 'confirm')}
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'InputObject', 'Login', 'ExcludeLogin', 'Database', 'ExcludeJobs', 'ExcludeDatabases', 'ExcludePassword', 'DefaultDatabase', 'Path', 'FilePath', 'Encoding', 'NoClobber', 'Append', 'BatchSeparator', 'DestinationVersion', 'NoPrefix', 'Passthru', 'EnableException'
[object[]]$knownParameters = 'SqlInstance', 'SqlCredential', 'InputObject', 'Login', 'ExcludeLogin', 'Database', 'ExcludeJobs', 'ExcludeDatabase', 'ExcludePassword', 'DefaultDatabase', 'Path', 'FilePath', 'Encoding', 'NoClobber', 'Append', 'BatchSeparator', 'DestinationVersion', 'NoPrefix', 'Passthru', 'EnableException'
$knownParameters += [System.Management.Automation.PSCmdlet]::CommonParameters
It "Should only contain our specific parameters" {
(@(Compare-Object -ReferenceObject ($knownParameters | Where-Object {$_}) -DifferenceObject $params).Count ) | Should Be 0
Expand Down Expand Up @@ -63,7 +63,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {

Context "Executes with Exclude Parameters" {
It "Should exclude databases when exporting" {
$file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabases -WarningAction SilentlyContinue
$file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabase -WarningAction SilentlyContinue
$results = Get-Content -Path $file -Raw
$allfiles += $file.FullName
$results | Should Match '\nGo\r'
Expand All @@ -75,7 +75,7 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
$results | Should Not Match 'Job'
}
It "Should exclude Go when exporting" {
$file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabases -BatchSeparator '' -WarningAction SilentlyContinue
$file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabase -BatchSeparator '' -WarningAction SilentlyContinue
$results = Get-Content -Path $file -Raw
$allfiles += $file.FullName
$results | Should Not Match 'Go'
Expand Down Expand Up @@ -113,25 +113,25 @@ Describe "$CommandName Integration Tests" -Tags "IntegrationTests" {
}
Context "Exports file to random and specified paths" {
It "Should export file to the configured path" {
$file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabases -WarningAction SilentlyContinue
$file = Export-DbaLogin -SqlInstance $script:instance2 -ExcludeDatabase -WarningAction SilentlyContinue
$results = $file.DirectoryName
$allfiles += $file.FullName
$results | Should Be $DefaultExportPath
}
It "Should export file to custom folder path" {
$file = Export-DbaLogin -SqlInstance $script:instance2 -Path $AltExportPath -ExcludeDatabases -WarningAction SilentlyContinue
$file = Export-DbaLogin -SqlInstance $script:instance2 -Path $AltExportPath -ExcludeDatabase -WarningAction SilentlyContinue
$results = $file.DirectoryName
$allfiles += $file.FullName
$results | Should Be $AltExportPath
}
It "Should export file to custom file path" {
$file = Export-DbaLogin -SqlInstance $script:instance2 -FilePath "$AltExportPath\Dbatoolsci_login_CustomFile.sql" -ExcludeDatabases -WarningAction SilentlyContinue
$file = Export-DbaLogin -SqlInstance $script:instance2 -FilePath "$AltExportPath\Dbatoolsci_login_CustomFile.sql" -ExcludeDatabase -WarningAction SilentlyContinue
$results = $file.Name
$allfiles += $file.FullName
$results | Should Be "Dbatoolsci_login_CustomFile.sql"
}
It "Should export file to custom file path and Append" {
$file = Export-DbaLogin -SqlInstance $script:instance2 -FilePath "$AltExportPath\Dbatoolsci_login_CustomFile.sql" -Append -ExcludeDatabases -WarningAction SilentlyContinue
$file = Export-DbaLogin -SqlInstance $script:instance2 -FilePath "$AltExportPath\Dbatoolsci_login_CustomFile.sql" -Append -ExcludeDatabase -WarningAction SilentlyContinue
$allfiles += $file.FullName
$file.CreationTimeUtc.Ticks | Should BeLessThan $file.LastWriteTimeUtc.Ticks
}
Expand Down

0 comments on commit cd917c4

Please sign in to comment.