powershellluhn

Powershell script involving AD users and luhn algorithm


I'm a Powershell newbie, and I need help with a script. I need to list all AD users with their employee ID, and I need a csv with a column which will state if the Employee ID is true or false according to Luhn algorithm test. I have this script:

import-module -Name ActiveDirectory

$SearchBaseOU = "OU=---,DC=---,DC=---"

$ADusers = Get-ADUser -Filter {Enabled -eq $True} -SearchBase $SearchBaseOU -Properties EmployeeID,CanonicalName,EmployeeNumber | 
   Where {$_.EmployeeID -eq "123456789"} | 
   Select-Object samAccountName, Name , EmployeeID, EmployeeNumber,@{name="OU";expression={(($_.CanonicalName.split('/'))[0..($_.CanonicalName.split('/').Count-2)]) -join '/'}}

$ADusers | Export-Csv .\EnabledInactiveUsers.csv -Force -NoTypeInformation -Encoding UTF8

This script lists all AD users with employee ID which equals to "123456789". I also found this script online:

https://www.powershellgallery.com/packages/gibbels-algorithms/1.0.3/Content/scripts%5Cluhn%5CTest-LuhnValidation.ps1

which tests if the ID is true or false.

My question is - How do I take the luhn algorithm function and add it to the original script above to make the test and export the results into true or false statements?

Thanks!


Solution

  • You can use PowerShell Calculated Properties

    For Example, see the last property in the select: "IsLuhn" like you used in the CanonicalName property in your example, Add the Luhn function from the link to your code first.

    $ADusers = Get-ADUser[... -Properties EmployeeID,CanonicalName,EmployeeNumber ...] | 
    Select EmployeeID,CanonicalName,EmployeeNumber,
    @{N="IsLuhn";E={Test-LuhnValidation $_.EmployeeID}}