Script help: Convert all computers in OU's static IPs to reservations

This topic contains 3 replies, has 4 voices, and was last updated by Profile photo of Brian B Brian B 1 year ago.

  • Author
  • #29880
    Profile photo of secretsysadmin

    I'm attempting to query all computers in one OU, grab their MAC and IP, create a DHCP reservation with that information, name it the computer's name, and then configure the server's IP address information to be DHCP and release/renew but I'm a bit stuck.

    Here's what I've got:

    Get-ADComputer -SearchBase "OU=Domain Servers,DC=Domain,DC=com" -Filter * | Select-Object Name,DNSHostName | export-csv c:\temp\servers.txt -NoTypeInformation -Encoding UTF8
    $List = Import-Csv C:\temp\servers.txt
    foreach ($Name in $List) {
        $ip = [System.Net.Dns]::GetHostAddresses($Name.Name)
        write-host $ip "    "
        $mac = Get-WmiObject win32_networkadapterconfiguration | select macaddress
        write-host $mac "       "

    1. This script seems to repeat the foreach loop on a specific server each time, that is not the first one in the list, and not the last one, so I'm very confused on that.
    2. It looks like the $mac returns as a hash table. Is there a way to get it to just list the macaddress of whichever adapter has $ip?
    3. Any guidance on the rest of this script would be amazing. I'm not sure if this script has already been created out there, if it has I'd love to not have to reinvent the wheel, but if not any pointers here to put me in the right direction would be phenomenal.

    I do really love powershell and just need some guidance and I should be able to figure the rest out. A script just seemed easier than manually converting 75 servers manually.

  • #29882
    Profile photo of Dan Potter
    Dan Potter

    You have to use the computername parameter otherwise you're just getting the local machine info .

    After that, you have to code for multiple nics.

  • #29914

    To get the MAC associate with the specific IP address you'll need something like:

    Get-WmiObject win32_networkadapterconfiguration -ComputerName $ip | Where-Object {$_.IPAddress -like $ip} | Select-Object -ExpandProperty MACAddress

    If your servers are 2012 or higher you could use Invoke-Command and use:

    Get-NetIPAddress $using:ip | Get-NetworkAdapter | Select-Object -ExpandProperty MACAddress
  • #30073
    Profile photo of Brian B
    Brian B
    $Servers = Get-ADComputer -SearchBase "OU=Terminal,OU=Server,OU=Machines,DC=fnb,DC=fnni,DC=com" -Filter * | Select-Object Name,DNSHostName #| export-csv c:\temp\servers.txt -NoTypeInformation -Encoding UTF8
    foreach ($Server in $Servers) {
        $ip = [System.Net.Dns]::GetHostAddresses($Server.Name)
        write-host $ip "    "
        $mac = Get-WmiObject win32_networkadapterconfiguration -ComputerName $Server.DNSHostName | where {$_.DefaultIPGateway -ne $null} | select -expand macaddress
        write-host $mac "       "

    EDIT: i just realized i ditched your CSV file creation in favor of a variable for my own testing. didn't want to write out a file, but your way is fine if you needed that for reference later.

    I tend to look for the NIC that has a default gateway configured. Though Matt's way would ensure you get the NIC with the IP you're looking for specifically.

    When you return results if you just want the string/value and not in an array/hash/etc... you can do a select and add the -expand (actually -ExpandProperty) which will net you just the value.

    Keep in mind, if this is in a flat network you'll be fine... likely, but if you have a layered environment with firewalls, etc... your results might not be perfect. Also, i'm assuming you're domain admin and have perms on every server you're planing on touching.

You must be logged in to reply to this topic.