powershell如何使用Windows方法将名称解析为IP地址 [英] powershell how to resolve name to IP address using Windows method

查看:61
本文介绍了powershell如何使用Windows方法将名称解析为IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到有人在用这个:

[Net.DNS]::GetHostEntry("serverName").addressList[0].IPAddressToString

...这对我有用.除了,如果我在 hosts 文件中有一个条目怎​​么办?这是否使用与 Windows 相同的方法或跳过 DNS 缓存和主机文件并始终查询 DNS 服务器?我想使用 Windows 用于浏览器、Windows 资源管理器、ping 等的任何方法.

...and that will work for me. Except, what if I have an entry in the hosts file? Does this use the same method that Windows uses or skip the DNS cache and hosts file and always query a DNS server? I would like to use whatever method Windows uses for browers, Windows Explorer, ping, etc.

推荐答案

为什么不直接使用内置的 DNS cmdlet?或者您是否有特殊原因沿着原始 .Net 路径旅行?代码项目、家庭作业、好奇心?

Why not just use the built-in DNS cmdlets? Or is there a particular reason you are traveling down the raw .Net path? Code project, homework assignment, curiosity?

 Get-Command -Name Resolve-Dns* | Format-Table -AutoSize

CommandType Name                   Version Source       
----------- ----                   ------- ------       
Cmdlet      Resolve-DnsName        1.0.0.0 DnsClient     



# Get parameters, examples, full and Online help for a cmdlet or function

(Get-Command -Name Resolve-DnsName).Parameters
Get-help -Name Resolve-DnsName -Examples
Get-help -Name Resolve-DnsName -Full
Get-help -Name Resolve-DnsName -Online


Resolve-DnsName -Name stackoverflow.com -Type A | Format-Table -AutoSize

Name              Type TTL Section IPAddress     
----              ---- --- ------- ---------     
stackoverflow.com A    268 Answer  151.101.65.69 
stackoverflow.com A    268 Answer  151.101.193.69
stackoverflow.com A    268 Answer  151.101.129.69
stackoverflow.com A    268 Answer  151.101.1.69  



Get-Content C:\WINDOWS\System32\Drivers\Etc\hosts

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost


Resolve-DnsName -Name localhost -Type ANY | Format-Table -AutoSize


Name              Type TTL  Section IPAddress
----              ---- ---  ------- ---------
localhost.lab.net A    1260 Answer  127.0.0.1



Resolve-DnsName -Name $env:COMPUTERNAME -Type ANY | Format-Table -AutoSize

Name Type TTL Section IPAddress                             
---- ---- --- ------- ---------                             
WS01 A    30  Answer  10....                            
WS01 AAAA 30  Answer  2601:...             
WS01 AAAA 30  Answer  2601:...
WS01 AAAA 30  Answer  fe80::...  

使用原始的 .Net 调用,会给你......

Using the raw .Net call, will give you ...

[Net.DNS]::GetHostEntry('stackoverflow.com').addressList[0].IPAddressToString
151.101.65.69

[Net.DNS]::GetHostEntry('localhost').addressList[0].IPAddressToString
::1

[Net.DNS]::GetHostEntry($env:COMPUTERNAME).addressList[0].IPAddressToString
fe80::...

这篇关于powershell如何使用Windows方法将名称解析为IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆