Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand.ExecuteCmdlet C# (CSharp) Method

ExecuteCmdlet() public method

public ExecuteCmdlet ( ) : void
return void
        public override void ExecuteCmdlet()
        {
            if (ParameterSetName == ContextParameterSet)
            {
                if (ShouldProcess(string.Format(Resources.ChangingContextUsingPipeline, Context.Tenant, Context.Subscription), 
                    Resources.ContextChangeWarning, string.Empty))
                {
                    AzureRmProfileProvider.Instance.Profile.SetContextWithCache(new AzureContext(Context.Subscription,
                        Context.Account,
                        Context.Environment, Context.Tenant));
                    CompleteContextProcessing();
                }
            }
            else if (ParameterSetName == SubscriptionNameParameterSet || ParameterSetName == SubscriptionIdParameterSet)
            {
                if (string.IsNullOrWhiteSpace(SubscriptionId)
                    && string.IsNullOrWhiteSpace(SubscriptionName)
                    && string.IsNullOrWhiteSpace(TenantId))
                {
                    throw new PSInvalidOperationException(Resources.SetAzureRmContextNoParameterSet);
                }

                    var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
                    if (!string.IsNullOrWhiteSpace(SubscriptionId) || !string.IsNullOrWhiteSpace(SubscriptionName))
                    {
                        if (ShouldProcess(string.Format(Resources.ChangingContextSubscription, 
                            SubscriptionName ?? SubscriptionId), 
                            Resources.SubscriptionChangeWarning , string.Empty))
                        {
                            profileClient.SetCurrentContext(SubscriptionId, SubscriptionName, TenantId);
                            CompleteContextProcessing();
                        }
                    }
                    else
                    {
                        if (ShouldProcess(string.Format(Resources.ChangingContextTenant, TenantId),
                            Resources.TenantChangeWarning, string.Empty))
                        {
                            profileClient.SetCurrentContext(TenantId);
                            CompleteContextProcessing();
                        }
                    }
            }
            else
            {
                CompleteContextProcessing();
            }

        }

Usage Example

        public void SelectAzureContextWithNoSubscriptionAndTenant()
        {
            var cmdlt = new SetAzureRMContextCommand();
            var tenantToSet = "72f988bf-86f1-41af-91ab-2d7cd011db47";

            // Setup
            cmdlt.CommandRuntime = commandRuntimeMock;

            // Make sure that the tenant ID we are attempting to set is
            // valid for the account
            var account = AzureRmProfileProvider.Instance.Profile.Context.Account;
            var existingTenants = account.GetProperty(AzureAccount.Property.Tenants);
            var allowedTenants = existingTenants == null ? tenantToSet : existingTenants + "," + tenantToSet;
            account.SetProperty(AzureAccount.Property.Tenants, allowedTenants);

            ((RuntimeDefinedParameterDictionary)cmdlt.GetDynamicParameters())["TenantId"].Value = tenantToSet;

            // Act
            cmdlt.InvokeBeginProcessing();
            cmdlt.ExecuteCmdlet();
            cmdlt.InvokeEndProcessing();

            // Verify
            Assert.True(commandRuntimeMock.OutputPipeline.Count == 1);
            var context = (PSAzureContext)commandRuntimeMock.OutputPipeline[0];

            // TenantId is not sufficient to change the context.
            Assert.NotEqual(tenantToSet, context.Tenant.TenantId);
        }
All Usage Examples Of Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand::ExecuteCmdlet