码迷,mamicode.com
首页 > 系统相关 > 详细

使用PowerShell脚本获取远程主机的IP配置信息

时间:2018-07-04 16:44:38      阅读:1043      评论:0      收藏:0      [点我收藏+]

标签:一个   pga   member   sub   pre   chord   def   dhcp   foreach   

使用PowerShell脚本获取远程主机的IP配置信息
  • 代码如下:

[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername
)

begin {}
process {
foreach ($Computer in $ComputerName) {
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32NetworkAdapterConfiguration -ComputerName $Computer | ? {$.IPEnabled}
foreach ($Network in $Networks) {
$IPAddress = $Network.IpAddress[0]
$SubnetMask = $Network.IPSubnet[0]
$DefaultGateway = $Network.DefaultIPGateway
$DNSServers = $Network.DNSServerSearchOrder
$IsDHCPEnabled = $false
If($network.DHCPEnabled) {
$IsDHCPEnabled = $true
}
$MACAddress = $Network.MACAddress
$OutputObj = New-Object -Type PSObject
$OutputObj | Add-Member -MemberType NoteProperty -Name ComputerName -Value $Computer.ToUpper()
$OutputObj | Add-Member -MemberType NoteProperty -Name IPAddress -Value $IPAddress
$OutputObj | Add-Member -MemberType NoteProperty -Name SubnetMask -Value $SubnetMask
$OutputObj | Add-Member -MemberType NoteProperty -Name Gateway -Value $DefaultGateway
$OutputObj | Add-Member -MemberType NoteProperty -Name IsDHCPEnabled -Value $IsDHCPEnabled
$OutputObj | Add-Member -MemberType NoteProperty -Name DNSServers -Value $DNSServers
$OutputObj | Add-Member -MemberType NoteProperty -Name MACAddress -Value $MACAddress
$OutputObj
}
}
}
}

end {}


步骤:

  1. 将上面代码保存为PS1文件。例如: GetIP.ps1

  2. 准备好一个远程主机的列表名文件(TXT文件格式),例如; Computer_List.txt

  3. 在PowerShell下执行下面的任意一个命令:
Get-Content Computer_List.txt | .\GetIP.ps1 | ft -AutoSize

或者

Get-Content Computer_List.txt | .\GetIP.ps1 | ft ComputerName, IPAddress, DNSServers -AutoSize

使用PowerShell脚本获取远程主机的IP配置信息

标签:一个   pga   member   sub   pre   chord   def   dhcp   foreach   

原文地址:http://blog.51cto.com/snowlai/2136150

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!