예제 #1
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                if (!this.IsParameterBound(c => c.EndDate))
                {
                    WriteVerbose(Resources.Properties.Resources.DefaultEndDateUsed);
                    EndDate = StartDate.AddYears(1);
                }

                if (this.IsParameterBound(c => c.ServicePrincipalObject))
                {
                    ObjectId = ServicePrincipalObject.Id;
                }

                if (this.IsParameterBound(c => c.ServicePrincipalName))
                {
                    ObjectId = ActiveDirectoryClient.GetObjectIdFromSPN(ServicePrincipalName);
                }

                if (Password != null && Password.Length > 0)
                {
                    string decodedPassword = SecureStringExtensions.ConvertToString(Password);
                    // Create object for password credential
                    var passwordCredential = new PasswordCredential()
                    {
                        EndDate   = EndDate,
                        StartDate = StartDate,
                        KeyId     = Guid.NewGuid().ToString(),
                        Value     = decodedPassword
                    };
                    if (ShouldProcess(target: ObjectId.ToString(), action: string.Format("Adding a new password to service principal with objectId {0}", ObjectId)))
                    {
                        WriteObject(ActiveDirectoryClient.CreateSpPasswordCredential(ObjectId, passwordCredential));
                    }
                }
                else if (this.IsParameterBound(c => c.CertValue))
                {
                    // Create object for key credential
                    var keyCredential = new KeyCredential()
                    {
                        EndDate   = EndDate,
                        StartDate = StartDate,
                        KeyId     = Guid.NewGuid().ToString(),
                        Value     = CertValue,
                        Type      = "AsymmetricX509Cert",
                        Usage     = "Verify"
                    };

                    if (ShouldProcess(target: ObjectId.ToString(), action: string.Format("Adding a new caertificate to service principal with objectId {0}", ObjectId)))
                    {
                        WriteObject(ActiveDirectoryClient.CreateSpKeyCredential(ObjectId, keyCredential));
                    }
                }
                else
                {
                    throw new InvalidOperationException("No valid keyCredential or passwordCredential to update!!");
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                if (!string.IsNullOrEmpty(ServicePrincipalName))
                {
                    ObjectId = ActiveDirectoryClient.GetObjectIdFromSPN(ServicePrincipalName);
                }

#pragma warning disable 0618
                if (!string.IsNullOrEmpty(Password))
#pragma warning restore 0618
                {
                    // Create object for password credential
                    var passwordCredential = new PasswordCredential()
                    {
                        EndDate   = EndDate,
                        StartDate = StartDate,
                        KeyId     = Guid.NewGuid().ToString(),
#pragma warning disable 0618
                        Value = Password
#pragma warning restore 0618
                    };
                    if (ShouldProcess(target: ObjectId, action: string.Format("Adding a new password to service principal with objectId {0}", ObjectId)))
                    {
                        WriteObject(ActiveDirectoryClient.CreateSpPasswordCredential(ObjectId, passwordCredential));
                    }
                }
                else if (!string.IsNullOrEmpty(CertValue))
                {
                    // Create object for key credential
                    var keyCredential = new KeyCredential()
                    {
                        EndDate   = EndDate,
                        StartDate = StartDate,
                        KeyId     = Guid.NewGuid().ToString(),
                        Value     = CertValue,
                        Type      = "AsymmetricX509Cert",
                        Usage     = "Verify"
                    };

                    if (ShouldProcess(target: ObjectId, action: string.Format("Adding a new caertificate to service principal with objectId {0}", ObjectId)))
                    {
                        WriteObject(ActiveDirectoryClient.CreateSpKeyCredential(ObjectId, keyCredential));
                    }
                }
                else
                {
                    throw new InvalidOperationException("No valid keyCredential or passwordCredential to update!!");
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                if (!this.IsParameterBound(c => c.EndDate))
                {
                    WriteVerbose(Resources.Properties.Resources.DefaultEndDateUsed);
                    EndDate = StartDate.AddYears(1);
                }

                if (this.IsParameterBound(c => c.ServicePrincipalObject))
                {
                    ObjectId = ServicePrincipalObject.Id;
                }

                if (this.IsParameterBound(c => c.ServicePrincipalName))
                {
                    ObjectId = ActiveDirectoryClient.GetObjectIdFromSPN(ServicePrincipalName);
                }

                if (this.IsParameterBound(c => c.CertValue))
                {
                    // Create object for key credential
                    var keyCredential = new KeyCredential()
                    {
                        EndDate   = EndDate,
                        StartDate = StartDate,
                        KeyId     = KeyId == default(Guid) ? Guid.NewGuid().ToString() : KeyId.ToString(),
                        Value     = CertValue,
                        Type      = "AsymmetricX509Cert",
                        Usage     = "Verify"
                    };

                    if (ShouldProcess(target: ObjectId, action: string.Format("Adding a new caertificate to service principal with objectId {0}", ObjectId)))
                    {
                        WriteObject(ActiveDirectoryClient.CreateSpKeyCredential(ObjectId, keyCredential));
                    }
                }
                else
                {
                    // If no credentials provided, set the password to a randomly generated GUID
                    var Password = Guid.NewGuid().ToString().ConvertToSecureString();

                    string decodedPassword = SecureStringExtensions.ConvertToString(Password);

                    var passwordCredential = new PasswordCredential()
                    {
                        EndDate   = EndDate,
                        StartDate = StartDate,
                        KeyId     = KeyId == default(Guid) ? Guid.NewGuid().ToString() : KeyId.ToString(),
                        Value     = decodedPassword
                    };
                    if (ShouldProcess(target: ObjectId, action: string.Format("Adding a new password to service principal with objectId {0}", ObjectId)))
                    {
                        var spCred    = new PSADCredentialWrapper(ActiveDirectoryClient.CreateSpPasswordCredential(ObjectId, passwordCredential));
                        spCred.Secret = Password;
                        WriteObject(spCred);
                    }
                }
            });
        }