public PSADApplication CreateApplication(CreatePSApplicationParameters createParameters)
        {
            IList<PasswordCredential> passwordCredentials = createParameters.PasswordCredentials != null
                ? createParameters.PasswordCredentials.Select(psCredential => psCredential.ToGraphPasswordCredential()).ToList()
                : null;

            IList<KeyCredential> keyCredentials = createParameters.KeyCredentials != null
                ? createParameters.KeyCredentials.Select(psCredential => psCredential.ToGraphKeyCredential()).ToList()
                : null;

            ApplicationCreateParameters graphParameters = new ApplicationCreateParameters
            {
                DisplayName = createParameters.DisplayName,
                Homepage = createParameters.HomePage,
                IdentifierUris = createParameters.IdentifierUris,
                PasswordCredentials = passwordCredentials,
                KeyCredentials = keyCredentials
            };

            return GraphClient.Application.Create(graphParameters).Application.ToPSADApplication();
        }
        public PSADApplication CreateApplication(CreatePSApplicationParameters createParameters)
        {
            IList<PasswordCredential> passwordCredentials = createParameters.PasswordCredentials != null
                ? createParameters.PasswordCredentials.Select(psCredential => psCredential.ToGraphPasswordCredential()).ToList()
                : null;

            IList<KeyCredential> keyCredentials = createParameters.KeyCredentials != null
                ? createParameters.KeyCredentials.Select(psCredential => psCredential.ToGraphKeyCredential()).ToList()
                : null;

            ApplicationCreateParameters graphParameters = new ApplicationCreateParameters
            {
                DisplayName = createParameters.DisplayName,
                Homepage = createParameters.HomePage,
                IdentifierUris = createParameters.IdentifierUris,
                ReplyUrls = createParameters.ReplyUrls,
                AvailableToOtherTenants = createParameters.AvailableToOtherTenants,
                PasswordCredentials = passwordCredentials,
                KeyCredentials = keyCredentials
            };

            try
            {
                return GraphClient.Applications.Create(graphParameters).ToPSADApplication();
            }
            catch (GraphErrorException ce)
            {
                if (ce.Response.StatusCode == HttpStatusCode.Forbidden)
                {
                    AADObject currentUser = GraphClient.Objects.GetCurrentUser();
                    if (currentUser != null && string.Equals(currentUser.UserType, "Guest", StringComparison.InvariantCultureIgnoreCase))
                    {
                        throw new InvalidOperationException(ProjectResources.CreateApplicationNotAllowedGuestUser);
                    }
                }

                throw;
            }
        }
        public override void ExecuteCmdlet()
        {
            CreatePSApplicationParameters createParameters = new CreatePSApplicationParameters
            {
                DisplayName = DisplayName,
                HomePage = HomePage,
                IdentifierUris = IdentifierUris,
                ReplyUrls = ReplyUrls,
                AvailableToOtherTenants = AvailableToOtherTenants
            };

            switch (ParameterSetName)
            {
                case ParameterSet.ApplicationWithPasswordPlain:
                    createParameters.PasswordCredentials = new PSADPasswordCredential[]
                    {
                        new PSADPasswordCredential
                        {
                            StartDate = StartDate,
                            EndDate = EndDate,
                            KeyId = Guid.NewGuid(),
                            Password = Password
                        }
                    };
                    break;

                case ParameterSet.ApplicationWithPasswordCredential:
                    createParameters.PasswordCredentials = PasswordCredentials;
                    break;

                case ParameterSet.ApplicationWithKeyPlain:
                    createParameters.KeyCredentials = new PSADKeyCredential[]
                    {
                        new PSADKeyCredential
                        {
                            StartDate = StartDate,
                            EndDate = EndDate,
                            KeyId = Guid.NewGuid(),
                            CertValue = CertValue
                        }
                    };
                    break;

                case ParameterSet.ApplicationWithKeyCredential:
                    createParameters.KeyCredentials = KeyCredentials;
                    break;
            }

            ExecutionBlock(() =>
            {
                if (ShouldProcess(target: createParameters.DisplayName, action: string.Format("Adding a new application with display name '{0}'", createParameters.DisplayName)))
                {
                    WriteObject(ActiveDirectoryClient.CreateApplication(createParameters));
                }
            });
        }
        protected override void ProcessRecord()
        {
            CreatePSApplicationParameters createParameters = new CreatePSApplicationParameters
            {
                DisplayName = DisplayName,
                HomePage = HomePage,
                IdentifierUris = IdentifierUris
            };

            switch (ParameterSetName)
            {
                case ParameterSet.ApplicationWithPasswordPlain:
                    createParameters.PasswordCredentials = new PSADPasswordCredential[]
                    {
                        new PSADPasswordCredential 
                        {
                            StartDate = StartDate,
                            EndDate = EndDate,
                            KeyId = Guid.NewGuid(),
                            Value = Password
                        }
                    };
                    break;

                case ParameterSet.ApplicationWithPasswordCredential:
                    createParameters.PasswordCredentials = PasswordCredentials;
                    break;

                case ParameterSet.ApplicationWithKeyPlain:
                    createParameters.KeyCredentials = new PSADKeyCredential[]
                    {
                        new PSADKeyCredential
                        {
                            StartDate = StartDate,
                            EndDate = EndDate,
                            KeyId = Guid.NewGuid(),
                            Type = KeyType,
                            Usage = KeyUsage,
                            Value = KeyValue
                        }
                    };
                    break;

                case ParameterSet.ApplicationWithKeyCredential:
                    createParameters.KeyCredentials = KeyCredentials;
                    break;
            }

            WriteObject(ActiveDirectoryClient.CreateApplication(createParameters));
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                if (ApplicationId == Guid.Empty)
                {
                    string uri = "http://" + DisplayName.Trim().Replace(' ', '_');

                    // Create an application and get the applicationId
                    CreatePSApplicationParameters appParameters = new CreatePSApplicationParameters
                    {
                        DisplayName = DisplayName,
                        IdentifierUris = new[] { uri },
                        HomePage = uri
                    };

                    if (ShouldProcess(target: appParameters.DisplayName, action: string.Format("Adding a new application for with display name '{0}'", appParameters.DisplayName)))
                    {
                        var application = ActiveDirectoryClient.CreateApplication(appParameters);
                        ApplicationId = application.ApplicationId;
                    }
                }

                CreatePSServicePrincipalParameters createParameters = new CreatePSServicePrincipalParameters
                {
                    ApplicationId = ApplicationId,
                    AccountEnabled = true
                };

                switch (ParameterSetName)
                {
                    case ParameterSet.ApplicationWithPasswordPlain:
                    case ParameterSet.DisplayNameWithPasswordPlain:
                        createParameters.PasswordCredentials = new PSADPasswordCredential[]
                        {
                        new PSADPasswordCredential
                        {
                            StartDate = StartDate,
                            EndDate = EndDate,
                            KeyId = Guid.NewGuid(),
                            Password = Password
                        }
                        };
                        break;

                    case ParameterSet.ApplicationWithPasswordCredential:
                    case ParameterSet.DisplayNameWithPasswordCredential:
                        createParameters.PasswordCredentials = PasswordCredentials;
                        break;

                    case ParameterSet.ApplicationWithKeyPlain:
                    case ParameterSet.DisplayNameWithKeyPlain:
                        createParameters.KeyCredentials = new PSADKeyCredential[]
                        {
                        new PSADKeyCredential
                        {
                            StartDate = StartDate,
                            EndDate = EndDate,
                            KeyId = Guid.NewGuid(),
                            CertValue = CertValue
                        }
                        };
                        break;

                    case ParameterSet.ApplicationWithKeyCredential:
                    case ParameterSet.DisplayNameWithKeyCredential:
                        createParameters.KeyCredentials = KeyCredentials;
                        break;
                }

                if (ShouldProcess(target: createParameters.ApplicationId.ToString(), action: string.Format("Adding a new service principal to be associated with an application having AppId '{0}'", createParameters.ApplicationId)))
                {
                    WriteObject(ActiveDirectoryClient.CreateServicePrincipal(createParameters));
                }
            });
        }