From 3a32d5e2b7e84e57cbb8c350dc8f5114299a3520 Mon Sep 17 00:00:00 2001 From: sqlarticles Date: Fri, 8 Sep 2023 09:28:17 +0100 Subject: [PATCH] bug fix 9068 --- public/New-DbaDbUser.ps1 | 8 +++++++- tests/New-DbaDbUser.Tests.ps1 | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/public/New-DbaDbUser.ps1 b/public/New-DbaDbUser.ps1 index d84f78c46c..bc4355b310 100644 --- a/public/New-DbaDbUser.ps1 +++ b/public/New-DbaDbUser.ps1 @@ -154,7 +154,8 @@ function New-DbaDbUser { $getDbParam.OnlyAccessible = $True if ($Database) { $getDbParam.Database = $Database - } elseif (-not $IncludeSystem) { + } + if (-not $IncludeSystem) { $getDbParam.ExcludeSystem = $True } if ($ExcludeDatabase) { @@ -169,6 +170,11 @@ function New-DbaDbUser { $databases = Get-DbaDatabase @getDbParam $getValidSchema = Get-DbaDbSchema -InputObject $databases -Schema $DefaultSchema -IncludeSystemSchemas + #This block is required so that correct error message can be returned to the user when incorrect database name is given or the database doesn't exists in the server. + if (-not $Database) { + $Database = $databases + } + foreach ($db in $Database) { $dbSmo = $databases | Where-Object Name -eq $db diff --git a/tests/New-DbaDbUser.Tests.ps1 b/tests/New-DbaDbUser.Tests.ps1 index 2350a3f129..c2dd233b16 100644 --- a/tests/New-DbaDbUser.Tests.ps1 +++ b/tests/New-DbaDbUser.Tests.ps1 @@ -85,5 +85,11 @@ Describe "$CommandName Integration Tests" -Tag "IntegrationTests" { $results.Name | Should -Be $loginName, $loginName, $loginName $results.DefaultSchema | Should -Be dbo, dbo, dbo } + + It "Should add user to all user databases" { + $results = New-DbaDbUser -SqlInstance $script:instance2 -Login $loginName -Force -EnableException + $results.Count | Should -Be 3 + $results.Name | Get-Unique | Should -Be $loginName + } } } \ No newline at end of file