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

Added check if database from $Database array is accessible or not before getting roles membership #5046

Merged
merged 8 commits into from
Feb 6, 2019
11 changes: 9 additions & 2 deletions functions/Get-DbaDbRoleMember.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ function Get-DbaDbRoleMember {
Stop-Function -Message 'Failure' -Category ConnectionError -ErrorRecord $_ -Target $instance -Continue
}

foreach ($item in $Database) {
Write-Message -Level Verbose -Message "Check if database: $item on $instance is accessible or not"
if ($server.Databases[$item].IsAccessible -eq $false) {
Stop-Function -Message "Database: $item is not accessible. Check your permissions or database state." -Category ResourceUnavailable -ErrorRecord $_ -Target $instance -Continue
}
}

$databases = $server.Databases | Where-Object { $_.IsAccessible -eq $true }

if (Test-Bound -Parameter 'Database') {
Expand Down Expand Up @@ -153,7 +160,7 @@ function Get-DbaDbRoleMember {
Add-Member -Force -InputObject $user -MemberType NoteProperty -Name UserName -Value $user.Name

# Select object because Select-DefaultView causes strange behaviors when assigned to a variable (??)
Select-Object -InputObject $user -Property 'ComputerName', 'InstanceName', 'SqlInstance', 'Database', 'Role', 'UserName', 'Login', 'IsSystemObject'
Select-Object -InputObject $user -Property 'ComputerName', 'InstanceName', 'SqlInstance', 'Database', 'Role', 'UserName', 'Login', 'IsSystemObject', 'LoginType'
}
}
}
Expand All @@ -163,4 +170,4 @@ function Get-DbaDbRoleMember {
end {
Test-DbaDeprecation -DeprecatedOn "1.0.0" -Alias Get-DbaRoleMember
}
}
}