I am very happy with this script. However, how could I go about logging the results of Copy-DBALogin to a file? I have tried a couple variations of start-transcript to no avail.
]]>Any help will be much appreciated
Regards
Anil
param (
# define the AG name
[Parameter(Mandatory=$true)][string]$AvailabilityGroupName,
[Parameter(Mandatory=$true)][string]$AvailabilityGroupListener,
[string]$ClientName = ‘AG Login Sync helper’
)
# internal variables
#$ClientName = ‘AG Login Sync helper’
$primaryInstance = $null
$secondaryInstances = @{}
try {
# connect to the AG listener, get the name of the primary and all secondaries
$replicas = Get-DbaAgReplica -SqlInstance $AvailabilityGroupListener -AvailabilityGroup $AvailabilityGroupName
$primaryInstance = $replicas | Where Role -eq Primary | select -ExpandProperty name
$secondaryInstances = $replicas | Where Role -ne Primary | select -ExpandProperty name
# create a connection object to the primary
$primaryInstanceConnection = Connect-DbaInstance $primaryInstance -ClientName $ClientName
# loop through each secondary replica and sync the logins
$secondaryInstances | ForEach-Object {
$secondaryInstanceConnection = Connect-DbaInstance $_ -ClientName $ClientName
Copy-DbaLogin -Source $primaryInstanceConnection -Destination $secondaryInstanceConnection -ExcludeSystemLogins -WhatIf
}
}
catch {
$msg = $_.Exception.Message
Write-Error “Error while syncing logins for Availability Group ‘$($AvailabilityGroupName): $msg'”
}
I have a hunch that you may be having many Logins on your primary replica with many explicit permissions. I’ve seen that once on another project that had nothing to do with dbatools and what I did back then was changing my code to use a longer timeout when querying the permissions – that did the trick for me. But it won’t be as easy here, since the exception is raised in one of the lower level functions of sync-dbalogin. Can you raise a bug report on dbatools?
]]>Login – WindowsUser mydomain\myuser Successful
Exception calling “EnumServerPermissions” with “1” argument(s): “There is
already an open DataReader associated with this Command which must be closed
first.”
At line:114615 char:9
+ $perms = $SourceServer.EnumServerPermissions($userName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : InvalidOperationException
The following exception occurred while trying to enumerate the collection:
“There is already an open DataReader associated with this Command which must
be closed first.”.
At line:114664 char:9
+ $loginCredentials = $SourceServer.Credentials | Where-Object …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], ExtendedTypeSystemExceptio
n
+ FullyQualifiedErrorId : ExceptionInGetEnumerator
Exception calling “EnumDatabaseMappings” with “0” argument(s): “Enumerate
database mappings failed for Login ‘mydomain\myuser’. ”
At line:114772 char:21
+ foreach ($db in $SourceLogin.EnumDatabaseMappings()) {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FailedOperationException
I don’t think that will happen soon. The reason is that I’d like to test code thoroughly before I put it up for public. I don’t do multi-AG instances and so I won’t be able to test it.
]]>