简体   繁体   中英

Powershell : Get remote computer name

I understand that to get computer name we can access to that server and execute this command via cmd.

在此处输入图片说明

But is it possible to get the remote computer name via ip address? all these ip address is internal ip.

This is not a PowerShell specific issue or limitation. This is a very common network admin thing to do.

As noted by BACON you can use nslookup, but Windows also provides .Net namespaces and PowerShell provides DNS cmdlets for this use case.

It has also been asked and answered many times on Stackoverflow. It's a very well documented thing on the MS docs site, TechNet, MSDN, and other blogs. For example

Powershell : Resolve Hostname from IP address and vice versa

# Find machine name from IP address:

$ipAddress= "192.168.1.54"
[System.Net.Dns]::GetHostByAddress($ipAddress).Hostname
Resolve Hostname to IP Address:
$machineName= "DC1"
$hostEntry= [System.Net.Dns]::GetHostByName($machineName)
$hostEntry.AddressList[0].IPAddressToString



<#
Resolve Hostname for set of IP addresses from text file:

Use the below powershell script to find machine name for multiple IP addresses. 
First create the text file ip-addresses.txt which includes one IP address in 
each line. You will get the machinename list in the txt file machinenames.txt.
#>

Get-Content C:\ip-addresses.txt | 
ForEach-Object{
    $hostname = ([System.Net.Dns]::GetHostByAddress($_)).Hostname
    if($? -eq $True) {
      $_ +": "+ $hostname >> "C:\machinenames.txt"
    }
    else {
       $_ +": Cannot resolve hostname" >> "C:\machinenames.txt"
    }
}


<#
Find Computer name for set of IP addresses from CSV:
Use the below powershell script to get hostname for multiple IP addresses from 
csv file. First create the csv file ip-addresses.csv which includes the column 
IPAddress in the csv file. You will get the hostname and IP address list in the 
csv file machinenames.csv.
#>

Import-Csv C:\ip-Addresses.csv | 
ForEach-Object{
    $hostname = ([System.Net.Dns]::GetHostByAddress($_.IPAddress)).Hostname

    if($? -eq $False){
    $hostname="Cannot resolve hostname"
    }

    New-Object -TypeName PSObject -Property @{
          IPAddress = $_.IPAddress
          HostName = $hostname
    }
} | 
Export-Csv 'D:\Temp\machinenames.csv' -NoTypeInformation -Encoding UTF8

There is also a very simple cmdlet Resolve-DnsName .

Resolve-DnsName 10.1.1.1

The result has a lot of detail. NameHost is what you want:

Name                           Type   TTL   Section    NameHost
----                           ----   ---   -------    --------
1.1.1.10.in-addr.arpa          PTR    3600  Answer     ServerName.DomainName.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM