Création script

This commit is contained in:
nicolas 2023-12-17 00:58:26 +01:00
parent 897acf751e
commit c86c8e5f24

View File

@ -0,0 +1,73 @@
#Script créé par Nicolas Lang - Sous licence CC-BY-SA
#https://nicolaslang.fr
function Give-time {
$retour = (Get-date).ToLongTimeString()
return $retour
}
$fullpathtofile = "$([environment]::getfolderpath("Desktop"))\export_ad_"+$((get-date).toshortdatestring().replace("/","-"))+".csv"
Write-Host "$(Give-time) : Ajout du module Active Directory"
try
{
ipmo activedirectory
}
catch
{
Write-Host "$(Give-time) : Attention! Impossible de récupérer le module A.D. pour Powershell. Assurez vous que les RSAT sont bien activés (si vous êtes sur un poste client) ou que le contrôleur de domaine sur lequel vous lancez ce script est correctement configuré"
Write-host "show_sidebar: false
visible: false
------------Fin du script dans 10 secondes---------------"
$x = 0
do
{
Write-Host -NoNewline "*"
Start-Sleep -Seconds 1
$x++
}until ($x -ge 10)
Exit
}
Write-Host "$(Give-time) : Obtention des données de l'A.D."
$users = Get-ADUser -filter * -Properties name,memberof | Sort-Object -Property memberof | Select-Object name,samaccountname,memberof
$max=0
Write-host "$(Give-time) : Calcul du maximum de groupes"
foreach ($user in $users)
{
if ($max -lt $($user.memberof).count)
{
$max = $(($user.memberof).count)
}
}
$array = new-object System.collections.arraylist
Write-Host "$(Give-time) : Analyse des données"
foreach ($user in $users)
{
$object = New-Object psobject
$object | Add-Member -MemberType NoteProperty -Name "Name" -Value $($user.name)
$object | Add-Member -MemberType NoteProperty -Name "SamAccountName" -Value $($user.samaccountname)
$groups = $($user.memberof) | % {$_.split(",")[0] -replace ("^CN=","")}
$grpnumber = 0
for ($start = 0; $start -lt $max;$start++)
{
if (!$groups -or !$groups[$start])
{
$object |Add-member -membertype Noteproperty -name "Group Number $($start+1)" -value ""
}
else
{
$object | Add-Member -MemberType NoteProperty -Name "Group Number $($start+1)" -value @($groups)[$start]
}
}
$array.Add($object) | Out-Null
}
Write-host "$(Give-time) : Fin de l'analyse des données. Export du fichier csv sur le bureau"
$array | sort-object -Property name | Export-Csv -Path $fullpathtofile -Encoding UTF8 -Force -NoTypeInformation -Delimiter ";"