{"action":"created","issue":{"url":"https://api.github.com/repos/sqlcollaborative/dbatools/issues/4867","repository_url":"https://api.github.com/repos/sqlcollaborative/dbatools","labels_url":"https://api.github.com/repos/sqlcollaborative/dbatools/issues/4867/labels{/name}","comments_url":"https://api.github.com/repos/sqlcollaborative/dbatools/issues/4867/comments","events_url":"https://api.github.com/repos/sqlcollaborative/dbatools/issues/4867/events","html_url":"https://github.com/sqlcollaborative/dbatools/issues/4867","id":393496507,"node_id":"MDU6SXNzdWUzOTM0OTY1MDc=","number":4867,"title":"-SecondarySqlCredential doesn't always work in Add-DbaAgDatabase","user":{"login":"RileyPrough","id":45948911,"node_id":"MDQ6VXNlcjQ1OTQ4OTEx","avatar_url":"https://avatars0.githubusercontent.com/u/45948911?v=4","gravatar_id":"","url":"https://api.github.com/users/RileyPrough","html_url":"https://github.com/RileyPrough","followers_url":"https://api.github.com/users/RileyPrough/followers","following_url":"https://api.github.com/users/RileyPrough/following{/other_user}","gists_url":"https://api.github.com/users/RileyPrough/gists{/gist_id}","starred_url":"https://api.github.com/users/RileyPrough/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RileyPrough/subscriptions","organizations_url":"https://api.github.com/users/RileyPrough/orgs","repos_url":"https://api.github.com/users/RileyPrough/repos","events_url":"https://api.github.com/users/RileyPrough/events{/privacy}","received_events_url":"https://api.github.com/users/RileyPrough/received_events","type":"User","site_admin":false},"labels":[{"id":638776746,"node_id":"MDU6TGFiZWw2Mzg3NzY3NDY=","url":"https://api.github.com/repos/sqlcollaborative/dbatools/labels/bugs_life","name":"bugs_life","color":"fc2929","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-12-21T15:25:58Z","updated_at":"2019-01-04T03:32:58Z","closed_at":null,"author_association":"NONE","body":"Below is a powershell script I've created to add a database to an availability group. The database is successfully added but sometimes the database owner on the secondary replica is my Windows account instead of \"sa\" like I'm specifying in the script with -SecondarySqlCredential. Sometimes the secondary database does get added with database owner of \"sa\" but I don't understand why it doesn't every time. Thoughts? I'm using the latest release of dbatools.\r\n```powershell\r\n$AvailabilityGroup = \"AG\"\r\n$PrimaryServer = \"Primary\"\r\n$SecondaryServer = \"Secondary\"\r\n$Database = \"Database\"\r\n$SharedPath = \"\\\\\" + $SecondaryServer + \"\\SQLBackups\"\r\n$Credential = Get-Credential \"sa\"\r\n\r\n$DatabaseBackupFile = $SharedPath + \"\\\" + $Database + \".bak\"\r\n$LogBackupFile = $SharedPath + \"\\\" + $Database + \".trn\"\r\n\r\nBackup-SqlDatabase -Database $Database -BackupFile $DatabaseBackupFile -ServerInstance $PrimaryServer\r\nBackup-SqlDatabase -Database $Database -BackupFile $LogBackupFile -ServerInstance $PrimaryServer -BackupAction Log\r\n\r\nRestore-SqlDatabase -Database $Database -BackupFile $DatabaseBackupFile -ServerInstance $SecondaryServer -NoRecovery\r\nRestore-SqlDatabase -Database $Database -BackupFile $LogBackupFile -ServerInstance $SecondaryServer -RestoreAction Log -NoRecovery\r\n\r\nAdd-DbaAgDatabase -AvailabilityGroup $AvailabilityGroup -Database $Database -Secondary $SecondaryServer -SecondarySqlCredential $Credential -SharedPath $SharedPath -SqlInstance $PrimaryServer -UseLastBackup\r\n```"},"comment":{"url":"https://api.github.com/repos/sqlcollaborative/dbatools/issues/comments/451345743","html_url":"https://github.com/sqlcollaborative/dbatools/issues/4867#issuecomment-451345743","issue_url":"https://api.github.com/repos/sqlcollaborative/dbatools/issues/4867","id":451345743,"node_id":"MDEyOklzc3VlQ29tbWVudDQ1MTM0NTc0Mw==","user":{"login":"wsmelton","id":11204251,"node_id":"MDQ6VXNlcjExMjA0MjUx","avatar_url":"https://avatars2.githubusercontent.com/u/11204251?v=4","gravatar_id":"","url":"https://api.github.com/users/wsmelton","html_url":"https://github.com/wsmelton","followers_url":"https://api.github.com/users/wsmelton/followers","following_url":"https://api.github.com/users/wsmelton/following{/other_user}","gists_url":"https://api.github.com/users/wsmelton/gists{/gist_id}","starred_url":"https://api.github.com/users/wsmelton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wsmelton/subscriptions","organizations_url":"https://api.github.com/users/wsmelton/orgs","repos_url":"https://api.github.com/users/wsmelton/repos","events_url":"https://api.github.com/users/wsmelton/events{/privacy}","received_events_url":"https://api.github.com/users/wsmelton/received_events","type":"User","site_admin":false},"created_at":"2019-01-04T03:32:58Z","updated_at":"2019-01-04T03:32:58Z","author_association":"MEMBER","body":"@RileyPrough in your example code you are not providing the `SqlCredential` on your restore commands, therefore the database is going to be restored under the user context of the PowerShell process (exe).\r\n\r\nA method that may help to keep this enforced:\r\n\r\n```powershell\r\n$AvailabilityGroup = \"AG\"\r\n$PrimaryServer = Connect-DbaInstance - SqlInstance \"Primary\" -SqlCredential (Get-Credential \"sa\")\r\n$SecondaryServer = Connect-DbaInstance -SqlInstance \"Secondary\" -SqlCredential (Get-Credential \"sa\")\r\n$Database = \"Database\"\r\n$SharedPath = \"\\\\\" + $SecondaryServer + \"\\SQLBackups\"\r\n\r\n$DatabaseBackupFile = $SharedPath + \"\\\" + $Database + \".bak\"\r\n$LogBackupFile = $SharedPath + \"\\\" + $Database + \".trn\"\r\n\r\nBackup-SqlDatabase -Database $Database -BackupFile $DatabaseBackupFile -ServerInstance $PrimaryServer\r\nBackup-SqlDatabase -Database $Database -BackupFile $LogBackupFile -ServerInstance $PrimaryServer -BackupAction Log\r\n\r\nRestore-SqlDatabase -Database $Database -BackupFile $DatabaseBackupFile -ServerInstance $SecondaryServer -NoRecovery\r\nRestore-SqlDatabase -Database $Database -BackupFile $LogBackupFile -ServerInstance $SecondaryServer -RestoreAction Log -NoRecovery\r\n\r\nAdd-DbaAgDatabase -AvailabilityGroup $AvailabilityGroup -Database $Database -Secondary $SecondaryServer -SharedPath $SharedPath -SqlInstance $PrimaryServer -UseLastBackup\r\n```"}}
{"actor":{"display_login":"wsmelton"}}