public override void ExecuteCmdlet()
{
if (!string.IsNullOrWhiteSpace(SubscriptionId) &&
!string.IsNullOrWhiteSpace(SubscriptionName))
{
throw new PSInvalidOperationException(Resources.BothSubscriptionIdAndNameProvided);
}
Guid subscrptionIdGuid;
if (!string.IsNullOrWhiteSpace(SubscriptionId) &&
!Guid.TryParse(SubscriptionId, out subscrptionIdGuid))
{
throw new PSInvalidOperationException(
string.Format(Resources.InvalidSubscriptionId, SubscriptionId));
}
AzureAccount azureAccount = new AzureAccount();
if (!string.IsNullOrEmpty(AccessToken))
{
if (string.IsNullOrWhiteSpace(AccountId))
{
throw new PSInvalidOperationException(Resources.AccountIdRequired);
}
azureAccount.Type = AzureAccount.AccountType.AccessToken;
azureAccount.Id = AccountId;
azureAccount.SetProperty(AzureAccount.Property.AccessToken, AccessToken);
}
else if (ServicePrincipal.IsPresent)
{
azureAccount.Type = AzureAccount.AccountType.ServicePrincipal;
}
else
{
azureAccount.Type = AzureAccount.AccountType.User;
}
if (!string.IsNullOrEmpty(CertificateThumbprint))
{
azureAccount.SetProperty(AzureAccount.Property.CertificateThumbprint, CertificateThumbprint);
}
SecureString password = null;
if (Credential != null)
{
azureAccount.Id = Credential.UserName;
password = Credential.Password;
}
if (!string.IsNullOrEmpty(ApplicationId))
{
azureAccount.Id = ApplicationId;
}
if (!string.IsNullOrEmpty(TenantId))
{
azureAccount.SetProperty(AzureAccount.Property.Tenants, new[] { TenantId });
}
#pragma warning disable 0618
if (ShouldProcess(string.Format(Resources.LoginTarget, azureAccount.Type, Environment.Name), "log in"))
{
if (AzureRmProfileProvider.Instance.Profile == null)
{
AzureRmProfileProvider.Instance.Profile = new AzureRMProfile();
}
var profileClient = new RMProfileClient(AzureRmProfileProvider.Instance.Profile);
WriteObject((PSAzureProfile) profileClient.Login(azureAccount, Environment, TenantId, SubscriptionId,
SubscriptionName, password));
}
#pragma warning restore 0618
}