gpt4 book ai didi

powershell - Powershell如何使用Windows方法将名称解析为IP地址

转载 作者:行者123 更新时间:2023-12-02 23:21:53 25 4
gpt4 key购买 nike

我看到有人在使用这个:

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

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

最佳答案

为什么不只使用内置的DNS cmdlet?
或者是您沿着原始的.Net路径旅行的特定原因吗?
代码项目,作业分配,好奇心?

 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调用,将为您提供...
[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 - Powershell如何使用Windows方法将名称解析为IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48778955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com