public override void ExecuteCmdlet() { AzureAccount azureAccount = new AzureAccount(); azureAccount.Type = ServicePrincipal.IsPresent ? AzureAccount.AccountType.ServicePrincipal : AzureAccount.AccountType.User; SecureString password = null; if (Credential != null) { azureAccount.Id = Credential.UserName; password = Credential.Password; } if (!string.IsNullOrEmpty(Tenant)) { azureAccount.SetProperty(AzureAccount.Property.Tenants, new[] {Tenant}); } var account = ProfileClient.AddAccountAndLoadSubscriptions(azureAccount, ProfileClient.GetEnvironmentOrDefault(Environment), password); if (account != null) { WriteVerbose(string.Format(Resources.AddAccountAdded, azureAccount.Id)); if (ProfileClient.Profile.DefaultSubscription != null) { WriteVerbose(string.Format(Resources.AddAccountShowDefaultSubscription, ProfileClient.Profile.DefaultSubscription.Name)); } WriteVerbose(Resources.AddAccountViewSubscriptions); WriteVerbose(Resources.AddAccountChangeSubscription); string subscriptionsList = account.GetProperty(AzureAccount.Property.Subscriptions); string tenantsList = account.GetProperty(AzureAccount.Property.Tenants); if (subscriptionsList == null) { WriteWarning(string.Format(Resources.NoSubscriptionAddedMessage, azureAccount.Id)); } WriteObject(account.ToPSAzureAccount()); } }
private AzureAccount MergeAccountProperties(AzureAccount account1, AzureAccount account2) { if (account1 == null || account2 == null) { throw new ArgumentNullException("account1"); } if (!string.Equals(account1.Id, account2.Id, StringComparison.InvariantCultureIgnoreCase)) { throw new ArgumentException("Account Ids do not match."); } if (account1.Type != account2.Type) { throw new ArgumentException("Account1 types do not match."); } AzureAccount mergeAccount = new AzureAccount { Id = account1.Id, Type = account1.Type }; // Merge all properties foreach (AzureAccount.Property property in Enum.GetValues(typeof(AzureAccount.Property))) { string propertyValue = account1.GetProperty(property) ?? account2.GetProperty(property); if (propertyValue != null) { mergeAccount.Properties[property] = propertyValue; } } // Merge Tenants var tenants = account1.GetPropertyAsArray(AzureAccount.Property.Tenants) .Union(account2.GetPropertyAsArray(AzureAccount.Property.Tenants), StringComparer.CurrentCultureIgnoreCase); mergeAccount.SetProperty(AzureAccount.Property.Tenants, tenants.ToArray()); // Merge Subscriptions var subscriptions = account1.GetPropertyAsArray(AzureAccount.Property.Subscriptions) .Union(account2.GetPropertyAsArray(AzureAccount.Property.Subscriptions), StringComparer.CurrentCultureIgnoreCase); mergeAccount.SetProperty(AzureAccount.Property.Subscriptions, subscriptions.ToArray()); return mergeAccount; }
public IEnumerable<AzureAccount> ToAzureAccounts() { if (!string.IsNullOrEmpty(ActiveDirectoryUserId)) { AzureAccount userAccount = new AzureAccount { Id = ActiveDirectoryUserId, Type = AzureAccount.AccountType.User }; userAccount.SetProperty(AzureAccount.Property.Subscriptions, new Guid(this.SubscriptionId).ToString()); if (!string.IsNullOrEmpty(ActiveDirectoryTenantId)) { userAccount.SetProperty(AzureAccount.Property.Tenants, ActiveDirectoryTenantId); } yield return userAccount; } if (!string.IsNullOrEmpty(ManagementCertificate)) { AzureAccount certificateAccount = new AzureAccount { Id = ManagementCertificate, Type = AzureAccount.AccountType.Certificate }; certificateAccount.SetProperty(AzureAccount.Property.Subscriptions, new Guid(this.SubscriptionId).ToString()); yield return certificateAccount; } }