13

I'm trying to write a script that depends on knowing the names of the computers on a network segment, but all the scripts I've found depend on a DNS inquiry which only replys with the names of a few of the machines. For example:

[System.Net.Dns]::GetHostbyAddress($IPAddress) 

I've also tried using

Ping -a $ipaddress

but this often fails to return the machine name as well. Is there a way to ask the host what it's name is directly and what level of permissions might be required in AD to get a response?
Thanks in advance.

Mog
  • 131
  • 1
  • 1
  • 3
  • 2
    DNS is the way to get IP > Name translations, the other option would be to remote into the machine with valid credentials and then check the hostname locally, if you're having issues retrieving the name via DNS, check it is using your DHCP/DNS and that you're querying the correct server, or that there is a manual DNS entry for the device otherwise. – colsw Jul 21 '17 at 16:03
  • 2
    NetBIOS might be an option, but DNS is definitely the way to go. – Mathias R. Jessen Jul 21 '17 at 16:14
  • Are you sure all the IP's are listed in your DNS? – Dave Jul 21 '17 at 16:16
  • Some network devices with IP addresses might not have records in DNS. Do all DHCP scopes create records in the same DNS? – Dave Jul 21 '17 at 16:18
  • Remoting by IP address isn't a walk in the park. See https://stackoverflow.com/q/6587426/562459. Does `nslookup your ip address` help at all? (I'd think not, but I've been surprised before.) – Mike Sherrill 'Cat Recall' Jul 21 '17 at 16:19
  • As other posters have mentioned DNS and WINS/NetBios are the two name services. You could look in your NetBios cache with `nbtstat`, but really DNS is the right technology for the job. Rather than trying to find the alternative lookup, resolving your DNS issue should be the first priority. I would check that the DHCP scope is set up to dynamically update DNS and it also dynamically updates clients that do not request updates. – BenH Jul 21 '17 at 17:18
  • 1
    Since you are using AD there is ONLY one way : DNS. AD relies on DNS. Problems with DNS will also affect AD and should always get fixed. – bluuf Jul 21 '17 at 19:51

6 Answers6

8

[System.Net.DNS]::GetHostByAddress() (now [System.Net.DNS]::GetHostEntry()) doesn't only rely on DNS, despite it's name. It will also check the local C:\Windows\System32\Drivers\etc\hosts file for locally configured entries.

straight dns via nslookup can't find the name:

PS C:\Users\Tim> nslookup 192.168.1.50
Server:  dns03
Address:  192.168.2.103

*** rpi03 can't find 192.168.1.50: Non-existent domain

yet, gethostentry() still finds the name:

PS C:\Users\Tim> [system.net.dns]::gethostentry('192.168.1.50')

HostName  Aliases AddressList
--------  ------- -----------
localentry {}      {192.168.1.50}
Tim Kennedy
  • 5,980
  • 1
  • 21
  • 16
3

COMMAND:

wmic.exe /node:10.20.30.40 OS get CSName /format:list 

BATCH FILE FOR WHOLE SUBNET:

for /L %%z in (1,1,254) do wmic.exe /node:10.20.30.%%z OS get CSName /format:list 2>NUL
  • This worked for some IP adresses but for some reason some other IP adresses didnt work. Instead the terminal gave me the message "RPC server unavailable" – Raul Chiarella Dec 23 '21 at 14:08
0

You can try by using something like: Invoke-Command -computername $computer {Get-Item HKLM:\SYSTEM\ControlSet001\Control\ComputerName\ActiveComputerName} The active computername is equal to your DNS name (without suffix ofcourse)

Niek
  • 1
  • 3
0

I may misunderstand the problem but you can query the Win32_ComputerSystem instance using a CIM session to the remote computer and use one of those properties (Name, DNSName, etc.) Running locally it would be like

Get-CimInstance -namespace root/cimv2 -classname Win32_ComputerSystem | fl *

I'm aware that WMI might take fairly hefty permissions (e.g., domain admin) but (a) that might not be out of the question for your use case and (b) you might be able to do some limited querying with fewer permissions.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
  • 1
    I tried using the Get-CimInstance command but do use it via IP address, the target machine (in this case the entire network) would need to be in the Trusted Hosts list. See error below:Get-CimInstance : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. – Mog Jul 24 '17 at 16:13
  • @Mog Did you try setting `-UseSSL` on the `CimSessionOption` and pointing at the `HTTPS` port (default 5986)? The error message seems to indicate you might be able to do that instead of adding computers to the trusted hosts list. – Patrick87 Jul 24 '17 at 16:47
  • Also, do any of the non-default authentication methods work for your use case? – Patrick87 Jul 24 '17 at 16:49
0

Another idea might be to query your SCCM server if you have one:

(Get-WmiObject -Query "SELECT * from SMS_R_SYSTEM WHERE IPAddresses LIKE '%$ipaddress%'" -Namespace "root\sms\site_$SiteCode" -computerName $SCCMServer).Name
DarkLite1
  • 13,637
  • 40
  • 117
  • 214
0

Another idea using powershell:

Get-WmiObject -Class Win32_ComputerSystem -ComputerName $Computer -Property Name | ForEach-Object {$_.Name}

Where $Computer is an IP address