0 votes

Hi,

I'm currently facing a problem where I want to set up a powershell script that should report all accounts (enabled, disabled, expired) matching a specific employeeType

Import-Module Adaxes

# Email message settings
$to = "ijacob@littelfuse.com" # TODO: modify me
$subject = "List of the users in lfext.com" # TODO: modify me
$htmlReportHeader = "<h3><b>List of the users in lfext.com </b></h3><br/>" # TODO: modify me
$htmlReportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me

$members = Get-AdmUser -Filter 'employeeType -like "extSharePoint"' -AdaxesService localhost -Server lfext.com -Properties *

# Build report
$disabledAccounts = "<b>Disabled Accounts</b><br/><ol>"
$expiredAccounts = "<b>Expired Accounts</b><br/><ol>"
$activeAccounts = "<b>Active Accounts</b><br/><ol>"

foreach ($member in $members){
    $currentDate = Get-Date
    #$accountExpires = $member.Get("accountExpires")

    if ($member.AccountDisabled)
    {
        $disabledAccounts += "<li>$member.name</li>"
        continue
    }
    elseif (($member.AccountExpirationDate -lt $currentDate))
    {
        $expiredAccounts += "<li>$member.name</li>"
        continue
    }
    else
    {
        $activeAccounts += "<li>$member.name</li>"
        continue
    }
}

# Build HTML report
$disabledAccounts += "</ol>"
$expiredAccounts += "</ol>"
$activeAccounts += "</ol>"
$htmlBody = $htmlReportHeader + $activeAccounts + $expiredAccounts + $disabledAccounts + $htmlReportFooter

# Send mail
$Context.SendMail($to, $subject, $NULL, $htmlBody)

Current Output:

Somehow expired and disabled Accounts are mixed up and I need to include the following fields:
· Company Name
· Name (DisplayName)
· Title
· Email address
· Phone/mobile
· CustomAttributeText10
· CustomAttributeText11
· CustomAttributeText12
· Userid expiration date
· Description
· Created Date
· Modified Date
And the header of the table should include the field names

The report does not need to contain any links to the Adaxes web service, full CN or something else, just the plain Info
Can you help me with this?

kind regards
Ingemar

by (960 points)
0

Meanwhile I changed the script to the following:

Import-Module Adaxes

# Email message settings
$to = "ijacob@littelfuse.com" # TODO: modify me
$subject = "List of the users in lfext.com" # TODO: modify me
$members = Get-AdmUser -Filter 'employeeType -like "extSharePoint"' -Properties * -AdaxesService localhost -Server lfext.com

$htmlBuilder = New-Object "System.Text.StringBuilder"
$htmlBuilder.append("<html><head>")
$htmlBuilder.append("<meta http-equiv=""Content-Type""`
    content=""text/html charset=UTF-8""></head>")
$htmlBuilder.append("<body>")
$htmlBuilder.appendFormat(
    "<p>Users in lfext.com (<b>{0}</b>)</p>",
    $members.count)
$htmlBuilder.append("<table width=""100%%"" border=""1"">")
$htmlBuilder.append("<tr>")
$htmlBuilder.append("<th>User Name</th>
    <th>Company</th><th>Title</th><th>Phone</th><th>Mobile</th><th>LF Contact</th><th>LF Title</th><th>LF Mail</th><th>Expiration Date</th><th>Description</th><th>Created</th><th>Modified</th>")
$htmlBuilder.append("</tr>")

foreach ($_ in $members) {

        $htmlBuilder.append("<tr>")       
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.Name)       
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.company)       
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.title) 
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.telephoneNumber)
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.mobile)
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.mobile) #.mobile is just a placeholder, should be adm-customAttributeText10
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.mobile) #.mobile is just a placeholder, should be adm-cumstomAttributeText11
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.mobile) #.mobile is just a placeholder, should be adm-cumstomAttributeText12
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.accountExpires) # doesn't look like a timestamp
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.description)  
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.createTimeStamp)
        $htmlBuilder.appendFormat("<td>{0}</td>", $_.modifyTimeStamp) 
        $htmlBuilder.append("</tr>")   
}

$htmlBuilder.append("</table>")
$htmlBuilder.append("</body></html>")
$Context.SendMail($to, $subject, $NULL,
    $htmlBuilder.ToString())

But I'm still facing few issues, phone numbers are not being displayed even so they are definitely filled in, I don't know how to retrieve the adm-CustomAttributeText fields, not sure how to get account expiration date and I'm missing the difference between active, disabled and already expired accounts.

right now, it looks like this:

0

finally I think I got most of my requirements by myself, here's the code:

Import-Module Adaxes

# Search all users in the target object
$searcher = $Context.TargetObject
$searcher.PageSize = 500
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchFilter = "(employeeType=extSharePoint)"
$searcher.SetPropertiesToLoad(@("userPrincipalName","cn","description","mobile","phoneNumber"))

# Email message settings
$to = "ijacob@littelfuse.com" # TODO: modify me
$subject = "List of the users in lfext.com" # TODO: modify me

$searchResult = $searcher.ExecuteSearch()

$htmlBuilder = New-Object "System.Text.StringBuilder"
$htmlBuilder.append("<html><head>")
$htmlBuilder.append("<meta http-equiv=""Content-Type""`
    content=""text/html charset=UTF-8""></head>")
$htmlBuilder.append("<body>")
$htmlBuilder.appendFormat(
    "<p>Users in lfext.com (<b>{0}</b>)</p>",
    $searchResult.count)
$htmlBuilder.append("<table width=""100%%"" border=""1"">")
$htmlBuilder.append("<tr>")
$htmlBuilder.append("<th>User Name</th>
    <th>Company</th><th>Title</th><th>Phone</th><th>Mobile</th><th>LF Contact</th><th>LF Title</th><th>LF Mail</th><th>Expiration Date</th><th>Description</th><th>Created</th><th>Modified</th>")
$htmlBuilder.append("</tr>")

foreach ($userID in $searchResult.FetchAll()) {

        $user = $Context.BindToObject($userID.AdsPath)

        $htmlBuilder.append("<tr>")       
        $htmlBuilder.appendFormat("<td>{0}</td>", $userID.Properties["cn"].Value)       
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("company"))       
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("title")) 
        $htmlBuilder.appendFormat("<td>{0}</td>", $userID.Properties["phoneNumber"].Value)
        $htmlBuilder.appendFormat("<td>{0}</td>", $userID.Properties["mobile"].Value)
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("adm-CustomAttributeText10"))
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("adm-CustomAttributeText11"))
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("adm-CustomAttributeText12"))
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("accountExpires")) # doesn't look like a timestamp
        $htmlBuilder.appendFormat("<td>{0}</td>", $userID.Properties["description"].Value)  
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("createTimeStamp"))
        $htmlBuilder.appendFormat("<td>{0}</td>", $user.Get("modifyTimeStamp")) 
        $htmlBuilder.append("</tr>")   
}

$htmlBuilder.append("</table>")
$htmlBuilder.append("</body></html>")
$Context.SendMail($to, $subject, $NULL,
    $htmlBuilder.ToString())

Only thing that's left so far, I need the Account Expiration Date and somehow I need to show which accounts are active, which disabled and which have expired, either by different row colors or somehow sorted.

kind regards
Ingemar

1 Answer

0 votes
by (215k points)
selected by
Best answer

Hello Ingemar,

We've made a script that can do the job and added it to our Script Repository. See Active, disabled and expired users with specific Employee Type. The script creates an HTML report with 3 different tables for active, disabled and expired user accounts.

The initial issue with scrambled active, disabled and expired accounts occurred because you tried to use the AccountDisabled property exposed by the IADsUser interface to determine whether an account is disabled. The interface is a part of Adaxes ADSI API. However, in your scripts, you use Adaxes PowerShell cmdlets. Instead of returning objects that support ADSI interfaces, the cmdlets return objects of the Softerra.Adaxes.PowerShellModule.Directory.ADUser type. Objects of that type do not expose the AccountDisabled property. You need to use the Enabled property of such objects instead. For more details, see:

Also, in your version of the script, you bound to the user account using $Context.BindToObject to get a user account attributes. Binding is an expensive operation. Instead, you can fetch the necessary property values during the search by passing the attribute names via the -Properties attribute of the Get-AdmUser cmdlet as it is done in our version of the script. This works much faster.

0

Thanks so much!

kinds regards
Ingemar

0

last question, if I want to add the manager to the table, which in itself is now issue, how do I retrieve the managers DisplayName?

kind regards
Ingemar

Related questions

0 votes
1 answer

Hi all, Am trying to convert a mailbox from a user box to a shared box in O365. Have been for the past few weeks even. The ... account the method points at - http://www.adaxes.com/sdk/?ExecuteScriptContextClass.html#executescriptcontext_getoffice365credential

asked Feb 24, 2020 by TheLexicon (180 points)
0 votes
1 answer

Hi, I try to make a report for our SAM to show all users with a specific license. But I fail to even find anything. I tried, among many ... .DirectorySearcher.AppendFilter("(adm-O365AccountLicenses=POWER_BI_STANDARD)") But I get nothing. Please advice.

asked May 20, 2021 by KristofferJ (80 points)
0 votes
1 answer

When running a PowerShell script as an action in a custom command, you can set the script to run as a different account and then use the RunAs property in the ... Is there another way to get the Adaxes service account's credentials from within the script?

asked Mar 31 by KelseaIT (320 points)
0 votes
1 answer

Is there a way to have a Scheduled Task with 4 different condition? I want to create a scheduled task start every Monday and the condition see: The next Saturday of the week ... of the week is the fifth of the month then no action Thanks in advance, Simone

asked Jan 18 by Simone.Vailati (270 points)
0 votes
1 answer

Hi, I followed this example: https://www.adaxes.com/sdk/IAdmTop6.html, but because the Custom Command is disabled, I get the following error message: System.Management.Automation ... if I enable the Custom Command. I am using Adaxes 2018.2 Best Regards Martin

asked Feb 19, 2020 by Martin (50 points)
2,807 questions
2,541 answers
6,615 comments
65,107 users