User Account Status

This script is a work in progress that pulls users from Unity along with relivent information for assessments including local vs AD account, if the mailbox has been initialized and if it is setup for Unified Messaging, Mailbox count and unread count/percent. This will be expanded as is goes. Scripts writes standard output to CSV. Because of the way information needs pulled from Unity this script takes a long time to run. Very long on large enviroments.


<?php
  //debug = true;  //uncomment for curl debugging
  $host     = "172.20.0.12";
  $username = "admin";
  $password = "Stryk3r10";

header("Content-Description: File Transfer"); 
header("Content-Type: text/csv"); 
header("Content-Disposition: attachment; filename=UnityExport.csv"); 


function getUsers($get) {

    global $host;
    global $username;
    global $password;
    global $debug;

    $ch=curl_init();

    ob_start();  
    $out = fopen('php://stderr', 'a+');
    
    if ( isset($debug) ) {
        curl_setopt($ch, CURLOPT_VERBOSE, true); 
        curl_setopt($ch, CURLOPT_STDERR, $out);   
    }
    
    $cupiurl="https://".$host.$get;

    curl_setopt( $ch, CURLOPT_URL, $cupiurl );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $result = curl_exec($ch);
    curl_close($ch);

    fclose($out);

    $cupi = json_decode(json_encode((array)simplexml_load_string($result)),1);

    return $cupi;
}
function printString($pass_value) {
    if ( ! empty($pass_value) ) {
            return "'".$pass_value."',";
        } else {
            return ",";
        }
}

$user_count = getUsers("/vmrest/users?rowsPerPage=10&pageNumber=0");
$user_count = $user_count['@attributes']['total'];

$user_pages = floor(($user_count / 10)+1);

echo "Alias,DtmfAccessId,FirstName,LastName,DisplayName,Department,LdapType,IsSetForVmEnrollent,UnifiedMessaging,Messages,Unread,Percent\n";
$page = 1;
while ( $page <= $user_pages ) {

    $cupi_array = getUsers("/vmrest/users?rowsPerPage=10&pageNumber=".$page);

    foreach ( $cupi_array['User'] as $user) {
        if ( @$user['Alias'] != '' ) {
            $user_detail = getUsers($user['URI']);
            $external_service = getUsers($user_detail['ExternalServiceAccountsURI']);
            $mailbox = getUsers("/vmrest/mailbox/folders/inbox/messages?userobjectid=".$user['URI']);
            $new_message=0;
            foreach ( $mailbox['Message'] as $message ) {
                if ( $message['Read'] == "false" ) {
                    $new_message++;
                }
            }
        $value  = "'".@$user_detail['Alias']."',";
        $value .= "'".@$user_detail['DtmfAccessId']."',";
        $value .= printString(@$user_detail['FirstName']);
        $value .= printString(@$user_detail['LastName']);
        $value .= printString(@$user_detail['DisplayName']);
        $value .= printString(@$user_detail['Department']);
        if ( @$user_detail['LdapType'] == 3 ){
            $value .= "'LDAP',";
        } else {
            $value .= "'Local',";
        }
        $value .= "'".@$user_detail['IsSetForVmEnrollment']."',";
        if ( @$external_service['@attributes']['total'] == 1 ) {
            $value .= "'true',";
        } else {
            $value .= "'false',";
        }
        $value .= "'".@$mailbox['@attributes']['total']."',";
        $value .= "'".@$new_message."',";
        if ( @$mailbox['@attributes']['total'] > 0 ) {
            $new_message = round( $new_message / @$mailbox['@attributes']['total'] * 100 );
        } else {
            $new_message = 0;
        }
        $value .= "'".$new_message."%'";

            echo $value."\n";
        }
    }
    $page++;
}
?>