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 $AD_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="172.16.0.0" because the other scopes concern dhcp relay and I had many errors.
Nice work, thank you !I made a small edit on the script, now it can be used with more than one scope in the DHCP server.
# Extraction de l'étendue $scope=Get-DhcpServerv4Scope | Select-Object ScopeId | ForEach-Object {$_.ScopeId} | ForEach-Object {$_.IPaddressToString} Write-Host "L'étendue est $scope" for ($i=0; $i -lt $scope.length; $i++){ # 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 $AD_station="$_" # Extraction de l'adresse MAC selon le nom de la station $DHCP_MAC=Get-DhcpServerv4Lease -computername $env:computername -allleases -ScopeId $scope[$i] | 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" } } }
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better 💗
Register Login