Created
July 30, 2025 00:56
-
-
Save Rapidhands/c2e1ba8b699b25212b347d4240d00abb to your computer and use it in GitHub Desktop.
Collecte des Compte machines dans l'AD avec IP et OS, et export dans un fichiers .xlsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import du module Active Directory | |
Import-Module ActiveDirectory | |
# Collecte des info sur les ordinateurs dans Active Directory avec les propriétés souhaitées | |
$AllComputers = Get-ADComputer -Filter * -Property Name, OperatingSystem, IPv4Address | |
# initialisation d'une liste pour stocker les résultats | |
$results = [System.Collections.Generic.List[PSCustomObject]]::new() | |
# Boucle à travers les ordinateurs et création d'un objet personnalisé pour chaque ordinateur | |
foreach ($computer in $AllComputers){ | |
$computerInfo = [PSCustomObject]@{ | |
Name = $computer.Name | |
OperatingSystem = $computer.OperatingSystem | |
IPv4Address = $computer.IPv4Address -join ', ' # Jonction des adresses IPv4 si plusieurs sont présentes | |
# OperatingSystemServicePack = $Computer.OperatingSystemServicePack | |
} | |
$results.add($computerInfo) | |
} | |
# Sélection des machines avec un OS spécifique | |
$Windows10 = $results | Where-Object { $_.OperatingSystem -like '*Windows 10*' } | |
$Windows11 = $results | Where-Object { $_.OperatingSystem -like '*Windows 11*' } | |
# on notera que les OS peuvent varier (ex. : Windows 10 Pro, Windows 10 Integral, Windows 10 Education, ...), donc on utilise -like pour une correspondance partielle et on utilisera le caractère joker "*" | |
# on peut exporter ces résultats dans des fichiers CSV séparés | |
$results | Export-Csv -Path 'C:\chemin\vers\export_computers.csv' -NoTypeInformation -Encoding UTF8 | |
$Windows10 | Export-Csv -Path 'C:\chemin\vers\export_windows10.csv' -NoTypeInformation -Encoding UTF8 | |
$Windows11 | Export-Csv -Path 'C:\chemin\vers\export_windows11.csv' -NoTypeInformation -Encoding UTF8 | |
# Export dans un fichier .xlsx avec le module ImportExcel | |
if (Get-Module -ListAvailable -Name ImportExcel){ | |
# Utilisation d'un splat pour mettre les paramètres communs (cela évite les répétitions et les lignes de commande longues) | |
$exportParams = @{ | |
Path = 'C:\chemin\vers\export_computers.xlsx' | |
AutoSize = $true | |
TableStyle = 'Medium6' | |
MaxAutoSizeRows = $true | |
AutoFilter = $true | |
FreezeTopRow = $true | |
} | |
$results | Export-Excel -WorksheetName 'Computers' | |
$Windows10 | Export-Excel -WorksheetName 'Windows10' | |
$Windows11 | Export-Excel -WorksheetName 'Windows11' | |
}else{ | |
Write-Host "Le module ImportExcel n'est pas installé. Veuillez l'installer pour exporter en format Excel." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment