Get IP Address and FQDN from Netbios name

This topic contains 4 replies, has 2 voices, and was last updated by  J. Mac 3 years, 6 months ago.

  • Author
    Posts
  • #15438

    J. Mac
    Participant

    Hello,

    I am attempting to write a script that would allow me to feed in a txt file with a list of NetBIOS names, query DNS to resolve the FQDN and related IP Address(es) and output it to an excel file. I'm just beginning with powershell, and there seem to be a few too many moving pieces for me to wrap my head around yet. It seemed like a simple task initially, but I'm banging my head against a wall.

    Any advice would be greatly appreciated.

    Cheers,
    J Mac

    PS – I'd also like any NetBIOS names that do NOT resolve to be shown in the final output.

  • #15552

    J. Mac
    Participant

    This function worked exactly as I needed it to. Thank you again for your help Mr. Robbins!

    The only issue I ran into is that the machine you run this script on will require that all the domain suffixes that might be in use be added to the DNS tab of your network adapter for the NetBIOS names to resolve properly.

    Thank you again for your help.

    Cheers,
    J Mac

  • #15439

    Mike F Robbins
    Participant

    Me personally, I would query a DHCP Server for the information. Here's a function that I wrote in about 5 minutes which should get you started on the right track if you choose to go that route and if your clients are receiving their addresses from a newer Windows Server that's providing the DHCP addresses:

    
    #Requires -Version 3.0
    #Requires -Modules DhcpServer
    function Get-DHCPInfo {
    
        [CmdletBinding()]
        param (
    
            [Parameter(ValueFromPipeline)]
            [ValidateNotNullorEmpty()]
            [string[]]$ComputerName = 'pc01',
    
            [ValidateNotNullorEmpty()]
            [string]$DHCPServer = 'dc01'
    
        )
    
        BEGIN {
    
            $ScopeInfo = Get-DhcpServerv4Scope -ComputerName $DHCPServer |
                         Get-DhcpServerv4Lease -ComputerName $DHCPServer
    
        }
    
        PROCESS {
            foreach ($Computer in $ComputerName) {
                $ScopeInfo |
                Where-Object hostname -like $ComputerBACKTICK.* |
                Select-Object -Property HostName, IPAddress
            }
        }
    
    }
    

    You can pipe the output of Get-Content on the txt file to this function. Note: there should be a backtick (escape character) on the Where-Object line between $Computer and the dot(.). The coding on this forum didn't like it.

  • #15464

    Mike F Robbins
    Participant

    I decided to take another shot at this by querying a DNS server for the information which is what you were looking for. This will also return the ones that don't resolve with nothing in any of the columns except the NetBIOSName one:

    
    #Requires -Version 3.0
    #Requires -Modules DNSClient
    function Get-MrDNSInfo {
    
    < #
    .SYNOPSIS
        Returns the FQDN and IP Address information for one or more computers. 
     
    .DESCRIPTION
        Get-MrDNSInfo is a function that returns the Fully Quailified Domain Name,
        along with IPv4 and IPv6 information for one or more computers which is
        retrieved from a Microsoft based DNS server.
     
    .PARAMETER ComputerName
        The computer(s) to retrieve FQDN and IP Address information for. The default is
        the local computer.
     
    .PARAMETER DNSServer
        The Microsoft based DNS Server to retrieve the FQDN and IP Address information
        from. The default is a DNS Server named DC01.
     
    .EXAMPLE
        Get-MrDNSInfo -ComputerName pc01, pc02, server01
     
    .EXAMPLE
         Get-MrDNSInfo -ComputerName pc01, pc02, server01 -DNSServer MyDNSServer
     
    .EXAMPLE
         'pc01', 'pc02', 'server01' | Get-MrDNSInfo
     
    .EXAMPLE
        Get-Content -Path .\Computers.txt | Get-MrDNSInfo -DNSServer MyDNSServer
     
    .INPUTS
        String
     
    .OUTPUTS
        PSCustomObject
     
    .NOTES
        Author:  Mike F Robbins
        Website: http://mikefrobbins.com
        Twitter: @mikefrobbins
    #>
    
        [CmdletBinding()]
        param (
    
            [Parameter(ValueFromPipeline)]
            [ValidateNotNullOrEmpty()]
            [string[]]$ComputerName = $env:COMPUTERNAME,
    
            [ValidateNotNullOrEmpty()]
            [string]$DNSServer = 'dc01'
    
        )
    
        PROCESS {
            foreach ($Computer in $ComputerName) {
                $DNSInfo = Resolve-DnsName -Server $DNSServer -Name $Computer -ErrorAction SilentlyContinue
    
                [PSCustomObject]@{    
                    NetBIOSName = $Computer
                    FQDN = $DNSInfo.Name | Get-Unique
                    IP4Address = $DNSInfo.IP4Address
                    IPV6Address = $DNSInfo.IP6Address
                }
    
            }
        }
    }
    
  • #15474

    J. Mac
    Participant

    Thank you very much Mr. Robbins, very much appreciated! I would also like to compliment you on your commenting. I'll send an update shortly when I have a chance to test this.

    Cheers,
    J Mac

You must be logged in to reply to this topic.