Ce script créé par mes soins permet de vérifier si les utilisateurs (logins stockés dans un fichier texte) existent dans l'AD.
Fonctionnalités :
- Vérifie la présence des utilisateurs (logins stockés dans un fichier texte) dans l'AD
- Création de 2 fichiers de logs horodatés et séparés (Users_OK & Users_KO)
- Exporter les données dans un fichier .csv
- Exporter les données dans un rapport HTML
Prérequis :
- Afin de pouvoir exécuter le script, il faut installer le Module Active Directory pour Windows PowerShell (Présent dans les [RSAT] : Outils d’administration de serveur distant pour Windows 7 et Windows 10)
Utilisation :
- Compléter le fichier Logins.txt avec les logins des comptes à rechercher sur l'AD
Exemple du fichier Logins.txt
avec les logins des comptes AD :
Administrateur Invité tic.tac Pierre krbtgt ben.nuts test1 test2 titi.grominet test3 test4 kit.kat GlenJohn d.guent
Screenshot :
Code du script :
<#
.SYNOPSIS
Find AD users.
.DESCRIPTION
Check if users exist in AD.
.NOTES
File name : Find-User.ps1
Author : Pierre JACQUOT
Date : 14/05/2016
Version : 1.0
.LINK
Website : https://www.pierrejacquot.yo.fr
Reference : https://www.pierrejacquot.yo.fr/index.php/scripts/26-script-find-user
#>
Clear-Host
Function Write-Log([string]$Output, [string]$Message) {
Write-Verbose $Message
((Get-Date -UFormat "[%d/%m/%Y %H:%M:%S] ") + $Message) | Out-File -FilePath $Output -Append -Force
}
$StartTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
[string]$Hostname = [Environment]::MachineName
[string]$Login = [System.Security.Principal.WindowsIdentity]::GetCurrent().Name
[string]$Workfolder = Split-Path $MyInvocation.MyCommand.Path
[string]$Date = Get-Date -UFormat "%Y-%m-%d"
[string]$TXTFile = $Workfolder + "\Logins.txt"
[string]$ReportFile = $Workfolder + "\$Date-Users-Report.html"
[string]$LogFileOK = $Workfolder + "\$Date-Users_OK.log"
[string]$LogFileKO = $Workfolder + "\$Date-Users_KO.log"
[array]$Logins = Get-Content -Path ".\Logins.txt" -ErrorAction SilentlyContinue
[int]$LineNumbers = $Logins.Count
[System.Collections.ArrayList]$UserList = @()
[string]$Activity = "Trying to launch the research of [$LineNumbers] user(s) in AD"
[int]$Step = 1
[string]$Title = "[$Date] - AD user(s) report on : $Hostname"
Write-Host "Find-User :" -ForegroundColor Black -BackgroundColor Yellow
Try {
Import-Module ActiveDirectory -ErrorAction Stop
Write-Host "ActiveDirectory module has been imported." -ForegroundColor Green
Write-Log -Output $LogFileOK -Message "ActiveDirectory module has been imported."
}
Catch {
Write-Warning "The ActiveDirectory module failed to load. Install the module and try again."
Write-Log -Output $LogFileKO -Message "The ActiveDirectory module failed to load. Install the module and try again."
Pause
Write-Host "`r"
Exit
}
If ((Test-Path ".\Logins.txt") -eq $False) {
Write-Warning "TXT file [Logins.txt] does not exist."
Write-Log -Output $LogFileKO -Message "TXT file [Logins.txt] does not exist."
}
ElseIf ($LineNumbers -eq 0) {
Write-Warning "TXT file [Logins.txt] is empty."
Write-Log -Output $LogFileKO -Message "TXT file [Logins.txt] is empty."
}
Else {
Write-Host "Launching the research of [$LineNumbers] user(s) in AD." -ForegroundColor Cyan
Write-Host "`r"
ForEach ($ADLogin in $Logins) {
[string]$Status = "Processing [$Step] of [$LineNumbers] - $(([math]::Round((($Step)/$LineNumbers*100),0)))% completed"
[string]$CurrentOperation = "Finding AD user : $ADLogin"
Write-Progress -Activity $Activity -Status $Status -CurrentOperation $CurrentOperation -PercentComplete ($Step/$LineNumbers*100)
$Step++
Start-Sleep -Seconds 1
Try {
$User = Get-ADUser -Filter { sAMAccountName -eq $ADLogin }
}
Catch {
[string]$ErrorMessage = $_.Exception.Message
Write-Host $ErrorMessage -ForegroundColor Red
Write-Host "`r"
Write-Log -Output $LogFileKO -Message $ErrorMessage
}
If ($User -eq $Null) {
[string]$UserExist = "KO"
Write-Host "$ADLogin - User does not exist in AD." -ForegroundColor Red
Write-Log -Output $LogFileKO -Message "$ADLogin - User does not exist in AD."
}
Else {
$UserExist = "OK"
Write-host "$ADLogin - User found in AD." -ForegroundColor Green
Write-Log -Output $LogFileOK -Message "$ADLogin - User found in AD."
}
$ServerObject = [PSCustomObject]@{
sAMAccountName = $ADLogin
Status = $UserExist
}
$UserList.Add($ServerObject) | Out-Null
}
}
$EndTime = Get-Date -Format "dd/MM/yyyy HH:mm:ss"
[decimal]$Duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalSeconds,2)
[string]$PreContent = "<h1>$Title</h1>
<h2>Number of AD user(s) : <span class='PostContentBlue'>$LineNumbers</span></h2>"
[string]$SuccessLogFile = "Success log file : <span class='PostContentBlue'>$(Split-Path $LogFileOK -Leaf)</span><br/>"
[string]$WarningLogFile = "Warning log file : <span class='PostContentBlue'>$(Split-Path $LogFileKO -Leaf)</span><br/>"
[string]$PostContent = "<p id='PostContent'>Script launched from : <span class='PostContentBlue'>$Hostname</span><br/>
By : <span class='PostContentBlue'>$Login</span><br/>
Path : <span class='PostContentBlue'>$Workfolder</span><br/>
TXT file : <span class='PostContentBlue'>$(Split-Path $TXTFile -Leaf)</span><br/>
Report file : <span class='PostContentBlue'>$(Split-Path $ReportFile -Leaf)</span><br/>
$(If ((Test-Path $LogFileOK) -eq $True) {
$SuccessLogFile
})
$(If ((Test-Path $LogFileKO) -eq $True) {
$WarningLogFile
})
Start time : <span class='PostContentBlue'>$StartTime</span><br/>
End time : <span class='PostContentBlue'>$EndTime</span><br/>
Duration : <span class='PostContentBlue'>$Duration</span> second(s)</p>"
[string]$Report = $UserList | ConvertTo-Html -As Table -CssUri ".\Style.css" -Title $Title -PreContent $PreContent -PostContent $PostContent
$Report = $Report -replace '<td>OK</td>','<td class="SuccessStatus">OK</td>'
$Report = $Report -replace '<td>KO</td>','<td class="CriticalStatus">KO</td>'
$Report | Out-File -FilePath $ReportFile -Encoding utf8
Write-Host "`r"
Write-Host "Script launched from : " -NoNewline; Write-Host $Hostname -ForegroundColor Red
Write-Host "By : " -NoNewline; Write-Host $Login -ForegroundColor Red
Write-Host "Path : " -NoNewline; Write-Host $Workfolder -ForegroundColor Red
Write-Host "TXT file : " -NoNewline; Write-Host (Split-Path $TXTFile -Leaf) -ForegroundColor Red
Write-Host "Report file : " -NoNewline; Write-Host (Split-Path $ReportFile -Leaf) -ForegroundColor Red
If ((Test-Path $LogFileOK) -eq $True) {
Write-Host "Log file : " -NoNewline; Write-Host (Split-Path $LogFileOK -Leaf) -ForegroundColor Red
}
If ((Test-Path $LogFileKO) -eq $True) {
Write-Host "Log file : " -NoNewline; Write-Host (Split-Path $LogFileKO -Leaf) -ForegroundColor Red
}
Write-Host "Start time : " -NoNewline; Write-Host $StartTime -ForegroundColor Red
Write-Host "End time : " -NoNewline; Write-Host $EndTime -ForegroundColor Red
Write-Host "Duration : " -NoNewline; Write-Host $Duration -ForegroundColor Red -nonewline; Write-Host " second(s)"
Write-Host "`r"
Exemple du fichier Users_OK.log
créé automatiquement avec les logins qui existent dans l'AD :
[09/05/2020 16:07:33] ActiveDirectory module has been imported. [09/05/2020 16:07:34] Administrateur - User found in AD. [09/05/2020 16:07:35] Invité - User found in AD. [09/05/2020 16:07:38] Pierre - User found in AD. [09/05/2020 16:07:39] krbtgt - User found in AD. [09/05/2020 16:07:41] test1 - User found in AD. [09/05/2020 16:07:42] test2 - User found in AD. [09/05/2020 16:07:44] test3 - User found in AD. [09/05/2020 16:07:45] test4 - User found in AD. [09/05/2020 16:07:48] GlenJohn - User found in AD. [09/05/2020 16:07:49] d.guent - User found in AD.
Exemple du fichier Users_KO.log
créé automatiquement avec les logins qui n'existent pas dans l'AD :
[09/05/2020 16:07:37] tic.tac - User does not exist in AD. [09/05/2020 16:07:40] ben.nuts - User does not exist in AD. [09/05/2020 16:07:43] titi.grominet - User does not exist in AD. [09/05/2020 16:07:46] kit.kat - User does not exist in AD.
Cliquer ici pour visualiser un exemple du rapport Users-Report.html
créé automatiquement.
Cliquer ici pour télécharger le fichier de style css.
Cliquer ici pour télécharger le script.