大多数 IP 地址可以用 Web Service 定位到物理地址。以下是一个很简单的函数,能够输入一个 IP 地址并返回它的物理地址:
1 2 3 4 5 6 7
#requires -Version 3.0
functionGet-IPLocation([Parameter(Mandatory)]$IPAddress) { Invoke-RestMethod-Method Get -Uri"http://geoip.nekudo.com/api/$IPAddress" | Select-Object-ExpandProperty Country -Property City, IP, Location }
name : United States code : US city : Mountain View ip : 8.8.8.8 location : @{accuracy_radius=1000; latitude=37,386; longitude=-122,0838; time_zone=America/Los_Angeles}
And here is how you can resolve any hostname to an IP address, for example, the famous powershellmagazine.com: 以下是如何将任意主机名解析成 IP 地址的代码,例如知名的 powershellmagazine.com:
name : United States code : US city : New York ip : 104.131.21.239 location : @{accuracy_radius=1000; latitude=40,7143; longitude=-74,006; time_zone=America/New_York}
(of course this is just where the server sits, not Aleksandar or Ravi or all the other fine editors (当然这只代表了服务器的所在地,而不是 Aleksandar or Ravi 及其它知名编辑的位置 )
PS> [System.Enum]::GetNames([System.ConsoleColor]) Black DarkBlue DarkGreen DarkCyan DarkRed DarkMagenta DarkYellow Gray DarkGray Blue Green Cyan Red Magenta Yellow White