找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 19040|回复: 3

[转载代码] [PowerShell每日技巧]根据主机名获取IP地址(20140128)

[复制链接]
发表于 2014-2-12 11:14:48 | 显示全部楼层 |阅读模式
There is a tiny .NET function called GetHostByName() that is vastly useful. It will look up a host name and return its current IP address:
  1. [System.Net.DNS]::GetHostByName('someName')
复制代码
With just a simple PowerShell wrapper, this is turned into a great little function that is extremely versatile:
  1. function Get-IPAddress
  2. {
  3.   param
  4.   (
  5.     [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  6.     [String[]]
  7.     $Name
  8.   )

  9.   process
  10.   { $Name | ForEach-Object { try { [System.Net.DNS]::GetHostByName($_) } catch { } }}
  11. }
复制代码
You can now run the function as-is (to get your own IP address). You can submit one or more computer names (comma separated). You can even pipe in data from Get-ADComputer or Get-QADComputer.
  1. Get-IPAddress
  2. Get-IPAddress -Name TobiasAir1
  3. Get-IPAddress -Name TobiasAir1, Server12, Storage1
  4. 'TobiasAir1', 'Server12', 'Storage1' | Get-IPAddress
  5. Get-QADComputer | Get-IPAddress
  6. Get-ADComputer -Filter * | Get-IPAddress
复制代码
This is possible because the function has both a pipeline binding and an argument serializer.

The -Name argument is fed to ForEach-Object, so no matter how many computer names a user specifies, they all get processed.

The -Name parameter accepts value from the pipeline both by property and as a value. So you can feed in any object that has a "Name" property, but you can also feed in any list of plain strings.

Note that the function has a very simple error handler. If you submit a computer name that cannot be resolved, nothing happens. Add code to the catch block if you want an error message instead.

http://powershell.com/cs/blogs/tips/archive/2014/01/28/getting-dns-ip-address-from-host-name.aspx
发表于 2014-2-12 15:56:25 | 显示全部楼层
有没有if的教程,谢谢!
发表于 2014-2-12 16:08:29 | 显示全部楼层
  1. http://marui.blog.51cto.com/1034148/293907
复制代码
多百度
 楼主| 发表于 2014-2-12 16:25:00 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 05:07 , Processed in 0.017949 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表