Microsoft Azure Password Reset Script

Following is an example script for resetting the account’s password on Microsoft Azure:

# Sample perl script for Password Reset of a user on Microsoft Azure Account 
 
## global variables 
my $retVal = 1; 
my $OS = $^O; 
my $cmd_output = ""; 
 
## arguments 
my $host = $args->arg("host"); 
my $port = $args->arg("port"); 
my $secure = $args->arg("secure"); 
my $admin = $args->arg("adminName"); 
my $adminPasswd = $args->arg("adminPasswd"); 
my $user = $args->arg("userName"); 
my $userPasswd = $args->arg("userPasswd"); 
my $Authority = $args->arg("Authority"); 
my $ClientID = $args->arg("ClientID"); 
my $AuthenticationTokenURL = $args->arg("AuthenticationTokenURL"); 
my $pam_home = getcwd;
 
# Set passwords as environment variables 
$ENV{ADMIN_PASSWORD} = $adminPasswd; 
$ENV{NEW_PASSWORD} = $userPasswd; 
 
$ctx->log_info("*** START Microsoft Azure PASSWD RESET"); 
$ctx->log_info("*** Privileged Account Manager running on the OS $OS at location $pam_home"); 
$ctx->log_info("Microsoft Azure System input parameters : Microsoft Azure Host - $host :: Port Number - $port :: Secure - $secure :: admin - $admin :: user - $user :: Authority - $Authority :: ClientID - $ClientID :: AuthenticationTokenURL - $AuthenticationTokenURL "); 
$ctx->log_info("Changing the password of the Microsoft Azure user $user ..."); 
 
## validate inputs 
if ($host eq "" or $port eq "" or $secure eq "" or $admin eq "" or $adminPasswd eq "" or $user eq "" or $userPasswd eq "" or $Authority eq ""  or $ClientID eq "" or $AuthenticationTokenURL eq "") { 
    $ctx->log_error("Incomplete inputs - following parameters are mandatory - Microsoft Azure host, port number, secure(1/0), keystone version, admin, adminPasswd, userName, userPasswd ,Authority name, ClientID name and AuthenticationTokenURL."); 
    return 0; 
} 
 
# Execute the java command for password reset 
if ($OS =~ "^MSWin") { 
    $cmd_output = `java -jar \"$pam_home\"/service/local/cmdctrl/lib/NPUM_AzureAD_api.jar $admin '$ClientID' '$AuthenticationTokenURL/$host/users/$user?api-version=1.6' $Authority $AuthenticationTokenURL`; 
} else { 
    my $point; 
    my @new_pwd = (); 
    my @pwd;; 
 
#escape single quote ''' in user password 
    @pwd = (); 
    @pwd = split(//, $userPasswd); 
        $point = 0; 
    foreach (@pwd){ 
        if($_ eq "'"){ 
            $new_pwd[$point++] = "'"; 
            $new_pwd[$point++] = '\\'; 
            $new_pwd[$point++] = "'"; 
            $new_pwd[$point++] = "'"; 
        } 
        else{ 
            $new_pwd[$point] = $_; 
        } 
        $point++; 
    } 
    $userPasswd = join("", @new_pwd); 
 
#escape single quote ''' in admin password 
    @pwd = (); 
    @new_pwd = (); 
    @pwd = split(//, $adminPasswd); 
        $point = 0; 
    foreach (@pwd){ 
        if($_ eq "'"){ 
            $new_pwd[$point++] = "'"; 
            $new_pwd[$point++] = '\\'; 
            $new_pwd[$point++] = "'"; 
            $new_pwd[$point++] = "'"; 
        } 
        else{ 
            $new_pwd[$point] = $_; 
        } 
        $point++; 
    } 
    $adminPasswd = join("", @new_pwd); 
 
    $cmd_output = `ADMIN_PASSWORD='$adminPasswd' NEW_PASSWORD='$userPasswd' java -jar /opt/netiq/npum/service/local/cmdctrl/lib/NPUM_AzureAD_api.jar $admin '$ClientID' '$AuthenticationTokenURL/$host/users/$user?api-version=1.6' $Authority $AuthenticationTokenURL`; 
} 
 
if ($? != 0) { 
    $ctx->log_error("Password reset for the user $user failed."); 
    $retVal = 0; 
} else { 
    $ctx->log_info("Successfully changed the password of the Microsoft Azure user $user ."); 
} 
 
$ctx->log_info("Command execution output as below : $cmd_output "); 
 
$ctx->log_info("*** END Microsoft Azure PASSWD RESET"); 
return $retVal;