azureazure-sql-databaseazure-powershellazure-data-sync

How to set value of -DefaultProfile (of type IAzureContextContainer) in New-AzSqlSyncMember


I am trying to create a powershell script to create azure data sync between 2 azure SQL databases. My member database is on another subscription.

I need to set -DefaultProfile which is of type on 'New-AzSqlSyncMember' command. I am not aware of the syntax for setting this parameter.

My current script without -DefaultProfile looks like below:

New-AzSqlSyncMember -ResourceGroupName $resourceGroupName `
                    -ServerName $serverName `
                    -DatabaseName $databaseName `
                    -SyncGroupName $syncGroupName `
                    -Name $syncMemberName `
                    -MemberDatabaseType $memberDatabaseType `
                    -SyncDirection $syncDirection

I want to set the value of the subscription field using powershell like in the image below using powershell:

Set Data Sync Member Subscription


Solution

  • Possible cross post from https://social.msdn.microsoft.com/Forums/en-US/4ad3dd3e-314a-4442-957f-da77c17ef85b/how-to-set-value-of-defaultprofile-of-type-iazurecontextcontainer-in-newazsqlsyncmember?forum=azurescripting&prof=required

    You want to call Connect-AzAccount with the credentials for the account you want to use in the -DefaultProfile parameter and store that in a variable. You can use that variable to set the parameter:

    $DefaultProfile = Connect-AzAccount <params> -SubscriptionId $SubscriptionId
    
    New-AzSqlSyncMember <params> -DefaultContext $DefaultProfile
    

    If this throws a type error that it can't convert from a PSAzureContext to a IAzureContextContainer there is an explicit converter available.

    $DefaultProfile = Connect-AzAccount <params> -SubscriptionId $SubscriptionId
    
    $Converter =  New-Object -TypeName Microsoft.Azure.Commands.Profile.Models.AzureContextConverter
    $Container =  $converter.ConvertFrom($DefaultProfile, [Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer], $null, $true)
    
    New-AzSqlSyncMember <params> -DefaultContext $Container