Script PowerShell for export DHCP MAC address and import to Active Directory

  • I just finished a powershell script to extract known MAC addresses from the DHCP server and import them into the Active Directory.

    The search for the range is automatic and before executing the script as it is you are invited to try it by commenting the line n°20 which starts with Set-ADComputer

    If you find improvements to be made, please indicate this by answering this subject.

    # Extraction de l'étendue	
    $scope=Get-DhcpServerv4Scope | Select-Object ScopeId | ForEach-Object {$_.ScopeId} | ForEach-Object {$_.IPaddressToString}
    Write-Host "L'étendue est $scope"
    # Liste des stations par nom
    Get-ADComputer -Filter {OperatingSystem -NotLike "*server*"} -Property Name | select-object -expandproperty name | ForEach-Object {
    	# Récupération du nom de la station
    	# Extraction de l'adresse MAC selon le nom de la station
    	$DHCP_MAC=Get-DhcpServerv4Lease -computername $env:computername -allleases -ScopeId $scope | Where-Object {$_.HostName -like "*$AD_station*"} | select-object -expandproperty ClientID
    	#Write-Host "L'adresse MAC de $AD_station est $DHCP_MAC"
    	# Ajout de l'adresse MAC dans l'attribut wWWHomePage
    	if($DHCP_MAC) {
    		# Set-ADComputer à commenter pour vos tests
    		Set-ADComputer $AD_station -HomePage $DHCP_MAC
    		Write-Host "L'adresse MAC $DHCP_MAC à été ajoutée dans l'AD à l'attribut wWWHomePage de la station $AD_station"

  • Hi ! I just replaced the second line with $scope="" because the other scopes concern dhcp relay and I had many errors.
    Nice work, thank you !