当服务器不响应ping时,通过powershell获取服务器的IP [英] Get the IP of a server through powershell when it doesnt respond to a ping

查看:330
本文介绍了当服务器不响应ping时,通过powershell获取服务器的IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于防火墙的限制,我处理的服务器没有回复ping命令,而是提供了IP地址

Due to firewall constraints, the servers I deal with do not reply to a ping, but do give their IP address

例如,如果我这样做:

ping myhost.com

我知道

Pinging myhost.com [192.1.1.1] with 32 bytes of data:
Request Timed Out.

如何从Powershell中获取服务器的IP?我知道如果服务器响应ping(测试连接或Ping主机)可以通过多种方式完成,但是当它忽略ping请求时我无法弄清楚

How can I just get the IP of the server from powershell? I know it can be done various ways if a server responds to a ping (Test-Connection or Ping-Host) , but I cant figure it when it ignores ping requests

推荐答案

您可以使用 System.Net.IPAddress 对象.

You can use the System.Net.Dns.GetHostAddresses method. This method will return an array of System.Net.IPAddress objects.

用法示例:

$ipAddresses = [System.Net.Dns]::GetHostAddresses('google.com')

列出所有地址:

$ipAddresses | %{$_.IPAddressToString}

获取数组中的第一个地址:

Grab the first address in the array:

$ipAddresses[0].IPAddressToString

这篇关于当服务器不响应ping时,通过powershell获取服务器的IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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