Users/SetOktaUserResetPassword.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Management.Automation;
using Okta.Core.Models;
 
namespace Okta.Core.Automation
{
    [Cmdlet(VerbsCommon.Set, "OktaUserResetPassword")]
    public class SetOktaUserResetPassword : OktaCmdlet
    {
        [Parameter(
            Mandatory = false,
            ValueFromPipelineByPropertyName = true,
            Position = 0,
            HelpMessage = "User Id to reset password for"
        )]
        public string Id { get; set; }
 
        [Parameter(
            Mandatory = false,
            ValueFromPipelineByPropertyName = true,
            Position = 2,
            HelpMessage = "$true to send password reset email, $false to return an activation link"
        )]
        public bool SendEmail { get; set; }
 
        protected override void ProcessRecord()
        {
            var usersClient = Client.GetUsersClient();
            System.Uri passwordResetUri = null;
            try
            {
                if (!string.IsNullOrEmpty(Id))
                {
                    passwordResetUri = usersClient.ForgotPassword(Id, SendEmail );
                }
 
                if (passwordResetUri != null)
                {
                    WriteObject(passwordResetUri);
                }
            }
            catch (OktaException oex)
            {
                ErrorRecord er = new ErrorRecord(oex, oex.ErrorId, ErrorCategory.InvalidData, usersClient);
                ErrorDetails errorDetails = new ErrorDetails(string.Format("An error occurred while resetting the password of the user: {0}", oex.ErrorSummary));
                er.ErrorDetails = errorDetails;
                WriteError(er);
            }
        }
    }
}