AD Enabled vs Disabled Users by searching AD email attribute

Hi Guys

I have had this script that works fine to query AD for users that exist in AD and or Not and if they are enabled or Disabled.


#Create a txt file with the AD usernames inside a txt file c:\Temp\userstatus\ named Balh.txt
#TXT FILE WOULD INCLUDE SAMACCOUNTNAME FROM AD ENVIRONMENT FOR EXAMPLE
#Blah01
#Blah02
#Blah03

$user = get-content C:\Temp\Blah.txt
$user | foreach {
$Name = "$_"
 $Searcher = [ADSISearcher]"(sAMAccountName=$Name)"
 $Results = $Searcher.FindOne()
 If ($Results -eq $Null) {"$Name not in AD" >> C:\Temp\userstatusblah.txt}
 Else {
 $status = (get-aduser $Name).enabled
 if ($status -eq "True"){
 
 "$Name is Enabled" >> C:\Temp\Blahuserstatus.txt}
 else{
 "$Name is Disabled" >> C:\Temp\Blahuserstatus.txt}
 }}
#If you want to check only enabled accounts delete the "#" below
 #get-content C:\userstatus\userstatus.txt | select-string "is Enabled" >> C:\userstatus\userstatus_enabled.txt
#If you want to check only disabled accounts delete the "#" below
get-content C:\Temp\Blahuserstatus.txt | select-string "is Disabled" >> C:\Temp\Blah_users_disabled_in_AD.txt
#If you want to check only accounts not in AD delete the "#" below
get-content C:\Temp\userstatusBlah.txt | select-string "not in AD" >> C:\Temp\Blah_users_not_in_AD.txt


This script works fine
However I need now read in the email address from the txt and have some code to lookup SAMaccountName from email address read in  to then resolve the SAMaccountName , to then use query ?

background:application does not have any common fields other than email address I can correlate.

How can i add this email address lookup for SAMaccountName for each user in txt file



#Create a txt file with the AD usernames inside a txt file c:\Temp\userstatus\ named Balh.txt
#TXT FILE WOULD INCLUDE SAMACCOUNTNAME FROM AD ENVIRONMENT FOR EXAMPLE
#Blah01@test.com.au
#Blah02@test.com.au
#Blah03@test.com.au
Johnathon WrightAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Johnathon WrightAuthor Commented:
I worked it out ,  not elegant but it works

two separate scripts :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jose Gabriel Ortega CastroCEOCommented:
you need to transform your TXT into a CSV,
Something like this:
username,email
UserA,userA@email.com

Open in new window


Then you can load the CSV in powershell like this:
Import-Csv C:\Temp\Blah.txt

Open in new window


The final solution would be something like this:
#require version 5

[CmdletBinding()]
param(
    [Parameter(Mandatory=$true,Position=0)]$CsvFile
)

class Alumno{
	[string]$UserName
    [string]$SamAccountName
	[string]$email
    [bool]$Exists
    [bool]$Enabled
}

 Import-module ActiveDirectory
$results=@()
$user = Import-Csv $CsvFile

$user | foreach{
    #Load AD module
   
    $Name=$_.Username
    $userSearch= Get-ADUser -Filter {Name -eq $Name -and ObjectClass -eq "user"} -Properties Mail,Enabled 


    $obj= New-Object Alumno


    if($userSearch){
        #exists
        Write-Host -ForegroundColor Green "User Exists $($userSearch.GivenName)"
        $obj.Username=$userSearch.GivenName
        $obj.SamAccountName = $userSearch.SamAccountName
        $obj.Email=$userSearch.Email
        $obj.Enabled=$userSearch.Enabled
        $obj.Exists=$true
    }
    else{
        #doesn't exists
        Write-Host -ForegroundColor Red "User does not exists  $Name"
        $obj.Username=$Name
        $obj.Exists=$false
        $obj.enabled=$false
    }
    
    $Results+=$obj
}

#All results into different outputs
$results | ConvertTo-Csv | Out-File Here.CSV
$results | ConvertTo-Html| Out-File here.html
$results | ConvertTo-json| Out-File here.json

#Not in AD ($exists -eq false)
$results| where{$_.Exists -eq $False}

#In ad
$results| where{$_.Exists -eq $true}

#Enabled
$results |where{$_.Enabled -eq $true} 

#Disabled
$results |where{$_.Enabled -eq $false} 

Open in new window

0
Johnathon WrightAuthor Commented:
I only know the email address at first , I then query to find SAMaccount name.  They cant exists in the same txt file at first
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Active Directory

From novice to tech pro — start learning today.