对于即将到来的Windows 10迁移,我需要一份MAC地址、IP地址和计算机名称的列表。现在我们有很多文本文件是在用户登录到网络时自动生成的。这些文本文件包含所需的信息。现在,我希望将这些信息合并到一个文件中。
这就是到目前为止的代码。
$p = @("Physikalische Adresse . . . . . . :","IPv4-Adresse . . . . . . . . . . :","Hostname . . . . . . . . . . . . :")
Get-ChildItem d:\pc\ -Filter *.txt |Get-Content |Select-String -Pattern $p |但是输出结果是这样的:
Hostname . . . . . . . . . . . . : Computername
Physikalische Adresse . . . . . . : MAC-address
IPv4-Adresse . . . . . . . . . . : IP-Address(Bevorzugt)
Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0但是我想要这样的输出:
计算机名Mac-Address IP-Adress
00-00-00-00-00-00-00-E0地址应该被过滤掉,因为它们是隧道适配器,所以不需要此信息。将一个文件的输出放在一行就足够了,这样我就可以使用excel进行过滤。
目前我有大约2000个文本文件。
文件内容示例:
10681EU;KREIS;Vico;\\DSFW1
Microsoft Windows [Version 6.1.7601]
Neue Verbindungen werden nicht gespeichert.
Status Lokal Remote Netzwerk
-------------------------------------------------------------------------------
OK F: \\i6633_nw\vol1\daten Microsoft Windows Network
OK J: \\i6633_nw\vol1\prog Microsoft Windows Network
OK K: \\keltenring1\vol1 Microsoft Windows Network
OK N: \\52.0.13.25\Kreisarchiv Microsoft Windows Network
OK O: \\i4811_nw_gw\vol1 Microsoft Windows Network
OK P: \\giga\vol1 Microsoft Windows Network
Der Befehl wurde erfolgreich ausgefhrt.
Windows-IP-Konfiguration
Hostname . . . . . . . . . . . . : 10681EU
Prim„res DNS-Suffix . . . . . . . : kreis.lan
Knotentyp . . . . . . . . . . . . : Hybrid
IP-Routing aktiviert . . . . . . : Nein
WINS-Proxy aktiviert . . . . . . : Nein
DNS-Suffixsuchliste . . . . . . . : kreis.lan
Ethernet-Adapter LAN-Verbindung:
Verbindungsspezifisches DNS-Suffix:
Beschreibung. . . . . . . . . . . : Realtek PCIe GBE Family Controller
Physikalische Adresse . . . . . . : 1C-6F-65-92-DE-06
DHCP aktiviert. . . . . . . . . . : Nein
Autokonfiguration aktiviert . . . : Ja
IPv4-Adresse . . . . . . . . . . : 52.0.103.215(Bevorzugt)
Subnetzmaske . . . . . . . . . . : 255.255.0.0
Standardgateway . . . . . . . . . : 52.0.1.1
DNS-Server . . . . . . . . . . . : 52.0.13.199
52.0.13.200
NetBIOS ber TCP/IP . . . . . . . : Aktiviert
Tunneladapter LAN-Verbindung* 9:
Medienstatus. . . . . . . . . . . : Medium getrennt
Verbindungsspezifisches DNS-Suffix:
Beschreibung. . . . . . . . . . . : Microsoft-6zu4-Adapter
Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0
DHCP aktiviert. . . . . . . . . . : Nein
Autokonfiguration aktiviert . . . : Ja
Tunneladapter LAN-Verbindung* 11:
Medienstatus. . . . . . . . . . . : Medium getrennt
Verbindungsspezifisches DNS-Suffix:
Beschreibung. . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0
DHCP aktiviert. . . . . . . . . . : Nein
Autokonfiguration aktiviert . . . : Ja发布于 2019-10-30 19:46:13
您可以使用以下代码来创建表示数据的对象的ArrayList。
1)创建包含来自所有文件的合并数据的ArrayList
2)使用Get-ChildItem从目录中读取文件
3)创建具有3个属性的psobject +使用-notlike和-match运算符使用数据填充属性。
假设:每个文件只有一个有效的IP地址/主机名/物理地址。
$all = New-Object System.Collections.ArrayList
Get-ChildItem c:\temp\ -Filter *.txt |
Foreach-Object {
$content = Get-Content $_.FullName
$obj = New-Object -TypeName psobject
$obj | Add-Member -MemberType NoteProperty -Name PhysicalAddress -Value ($content | where-object {($_ -notlike "*00-00-00-00-00*" -and $_ –match "Physikalische Adresse")} | foreach-object{$_.Split(":")[1].Trim()} | Select -First 1)
$obj | Add-Member -MemberType NoteProperty -Name Hostname -Value ($content | where-object {($_ –match "HostName")} | foreach-object{$_.Split(":")[1].Trim()} | Select -First 1)
$obj | Add-Member -MemberType NoteProperty -Name IPAddress -Value ($content | where-object {($_ –match "IPv4-Adresse")} | foreach-object{$_.Split(":")[1].Trim()} | Select -First 1)
$all.Add($obj)
}
$all | Format-List输出(数组):
PhysicalAddress : 1C-6F-65-92-DE-06
Hostname : 10681EU
IPAddress : 52.0.103.215https://stackoverflow.com/questions/58620736
复制相似问题