micneu
Goto Top

Powershell in datei suchen und inhalt in neue datei speichern

Hallo Leute,

ich habe ein problem, ich habe ca. 150 dateien mit ähnlichem inhalt in einem verzeichnis.

in diesen dateien soll es bestimmte inhalte ausgelesen werden und in einer neuen datei als .csv gespeichert werden.

hier ein aufbau einer datei:

Windows-IP-Konfiguration

   Hostname  . . . . . . . . . . . . : msc-4
   Prim„res DNS-Suffix . . . . . . . : 
   Knotentyp . . . . . . . . . . . . : Hybrid
   IP-Routing aktiviert  . . . . . . : Nein
   WINS-Proxy aktiviert  . . . . . . : Nein

Ethernet-Adapter Bluetooth-Netzwerkverbindung:

   Medienstatus. . . . . . . . . . . : Medium getrennt
   Verbindungsspezifisches DNS-Suffix: 
   Beschreibung. . . . . . . . . . . : Bluetooth-Ger„t (PAN)
   Physikalische Adresse . . . . . . : 00-15-FF-19-19-15
   DHCP aktiviert. . . . . . . . . . : Ja
   Autokonfiguration aktiviert . . . : Ja

Ethernet-Adapter LAN-Verbindung:

   Verbindungsspezifisches DNS-Suffix: 
   Beschreibung. . . . . . . . . . . : Realtek RTL8168C(P)/8111C(P) Family PCI-E Gigabit Ethernet NIC (NDIS 6.20)
   Physikalische Adresse . . . . . . : 00-FF-85-1B-AC-69
   DHCP aktiviert. . . . . . . . . . : Nein
   Autokonfiguration aktiviert . . . : Ja
   Verbindungslokale IPv6-Adresse  . : fe80::cdd7:c072:288:b94b%11(Bevorzugt) 
   IPv4-Adresse  . . . . . . . . . . : 192.168.23.11(Bevorzugt) 
   Subnetzmaske  . . . . . . . . . . : 255.255.255.0
   Standardgateway . . . . . . . . . : 192.168.23.1
   DHCPv6-IAID . . . . . . . . . . . : 234889605
   DHCPv6-Client-DUID. . . . . . . . : 00-01-00-01-13-0B-55-D5-00-21-85-1B-AC-69
   DNS-Server  . . . . . . . . . . . : 192.168.23.1
   NetBIOS ber TCP/IP . . . . . . . : Aktiviert

Tunneladapter isatap.{25C5AFF5-770E-4378-8DAE-628EB1DB977E}:

   Medienstatus. . . . . . . . . . . : Medium getrennt
   Verbindungsspezifisches DNS-Suffix: 
   Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter
   Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP aktiviert. . . . . . . . . . : Nein
   Autokonfiguration aktiviert . . . : Ja

Tunneladapter LAN-Verbindung* 4:

   Verbindungsspezifisches DNS-Suffix: 
   Beschreibung. . . . . . . . . . . : Teredo Tunneling Pseudo-Interface
   Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP aktiviert. . . . . . . . . . : Nein
   Autokonfiguration aktiviert . . . : Ja
   IPv6-Adresse. . . . . . . . . . . : 2001:0:5ef5:73bc:c79:db4:abd1:ecab(Bevorzugt) 
   Verbindungslokale IPv6-Adresse  . : fe80::c79:db4:abd1:ecab%13(Bevorzugt) 
   Standardgateway . . . . . . . . . : ::
   NetBIOS ber TCP/IP . . . . . . . : Deaktiviert

Tunneladapter isatap.{D76940D6-8ED4-440F-9153-58B2AAC1847F}:

   Medienstatus. . . . . . . . . . . : Medium getrennt
   Verbindungsspezifisches DNS-Suffix: 
   Beschreibung. . . . . . . . . . . : Microsoft-ISATAP-Adapter #2
   Physikalische Adresse . . . . . . : 00-00-00-00-00-00-00-E0
   DHCP aktiviert. . . . . . . . . . : Nein
   Autokonfiguration aktiviert . . . : Ja
ich möchte aus allen ethernet adaptern die mac-adresse, ip-adresse und den hostnamen
in einer .csv datei für jede datei eine zeile.
z. B.: IP;MAC;HOSTNAME
192.168.23.11;00-FF-85-1B-AC-69;msc-4

wie bekomme ich es in powershell hin diese daten
mit meinem code bekomme ich es noch nicht wirklich hin....
$path = "C:\"  
$endung = "*.txt"  
Get-ChildItem $path -include $endung -Recurse |
Select-String -Pattern "Physikalische Adresse"  

2. punkt wie bekomme ich es hin hat eine datei mehrere netzwerkkarten das jede NIC einen einzelnen eintrag in der
datei bekommt?

3. es ist nicht immer windows 7 oder vista, es können auch windows 200 und xp dabei sein...
also dort ist die datei etwas anderst

Windows-IP-Konfiguration



        Hostname. . . . . . . . . . . . . : MFC-PC-111

        Primäres DNS-Suffix . . . . . . . : 

        Knotentyp . . . . . . . . . . . . : Unbekannt

        IP-Routing aktiviert. . . . . . . : Nein

        WINS-Proxy aktiviert. . . . . . . : Nein



Ethernetadapter LAN-Verbindung 3:



        Verbindungsspezifisches DNS-Suffix: 

        Beschreibung. . . . . . . . . . . : Broadcom NetLink (TM) Gigabit Ethernet

        Physikalische Adresse . . . . . . : 00-19-99-31-97-AA

        DHCP aktiviert. . . . . . . . . . : Nein

        IP-Adresse. . . . . . . . . . . . : 192.168.1.111

        Subnetzmaske. . . . . . . . . . . : 255.255.255.0

        Standardgateway . . . . . . . . . : 192.168.1.200

gruß michael

Content-Key: 136339

Url: https://administrator.de/contentid/136339

Ausgedruckt am: 28.03.2024 um 10:03 Uhr

Mitglied: 76109
76109 19.02.2010 um 13:34:31 Uhr
Goto Top
Hallo Michael!

Powershell ist nicht mein Gebiet, aber alternativ kannst Du ja mal dieses VB-Skript testen (*.vbs).

Zur Funktion:
Alle Doppelpunkte + Leerzeichen durch einen Zeilenvorschub ersetzen
Anschließend die Text-Datei Zeilenvorschub-gesplittet in ein Array einlesen
Array durchsuchen nach: Hostname (Name = Array(i+1))
Array weiterdurchsuchen nach: Ethernet-Adapter LAN OR Ethernetadapter LAN
Wenn gefunden: Adapter = True
Array weiterdurchsuchen nach: Physikalische Adresse (Adresse = Array(i+1))
Array weiterdurchsuchen nach: IP-Adresse (Adresse = Array(i+1))
Variablen-Array prüfen Physikalische Adresse <> "" AND IP-Adresse <> ""
Array mit Trennzeichen (Semikolon) in Csv-Datei schreiben.

Quellcode kopieren und unter *.vbs abspeichern:
Const TxtType = "txt"  
Const TxtPath = "X:\Test"  
Const CsvPath = "X:\Test\Test.Csv"  

Const Adapter1 = "Ethernet-Adapter LAN"  
Const Adapter2 = "Ethernetadapter LAN"  


Dim Fso, File, TxtFile, CsvFile, Adapter, Text, Token, i

Set Fso = CreateObject("Scripting.FileSystemObject")  

Set CsvFile = Fso.CreateTextFile(CsvPath)
            
For Each File In Fso.GetFolder(TxtPath).Files
    If LCase(Fso.GetExtensionName(File)) = LCase(TxtType) Then
        Set TxtFile = Fso.OpenTextFile(File.Path)
            
        Text = Split(Replace(TxtFile.ReadAll, ": ", vbCrLf), vbCrLf):  TxtFile.Close  
            
        Adapter = False:  Token = Array("", "", "")  
            
        For i = 0 To UBound(Text)
            If InStr(1, Text(i), "Hostname", vbTextCompare) > 0 Then  
                Token(2) = Text(i + 1)
            ElseIf Token(2) <> "" Then  
                If InStr(1, Text(i), Adapter1, vbTextCompare) > 0 Or InStr(1, Text(i), Adapter2, vbTextCompare) > 0 Then
                    Adapter = True
                ElseIf Adapter Then
                    If Left(LTrim(Text(i)), 6) = "Physik" Then  
                        Token(1) = Text(i + 1)
                    ElseIf Left(LTrim(Text(i)), 2) = "IP" Then  
                        Token(0) = Split(Text(i + 1), "(")(0)  
                    End If
                    If Token(0) <> "" And Token(1) <> "" Then CsvFile.WriteLine Join(Token, ";"):  Exit For  
                End If
            End If
        Next
    End If
Next
CsvFile.Close

Gruß Dieter
Mitglied: micneu
micneu 21.02.2010 um 12:29:37 Uhr
Goto Top
hallo leute habe jetzt ein ergebnis (langsam) und ein neues problem.
ich habe es soweit das es so funktioniert wie ich es haben wollte nur hatte ich nicht bedacht das auch einige computer
auch mehrere netzwerk schnittstellen haben und die würde ich auch gerne dem hostnamen zuordnen.

hier mein quelltext:

#   **************************************************************
#    einmal auf der powershell ausgeführt werden
#     "Set-ExecutionPolicy Unrestricted" 
#   **************************************************************
#    
#    
#   **************************************************************
#   * Erstellt M.N. - POWERSHELL / Entwicklung   21.02.2010      
#   **************************************************************
# variablen deklarieren
$start = Get-Date
$ziel = "C:\Users\mne\Desktop\07_ips.txt"  
$endung = "*.txt"  
dir $ziel -filter $endung | 
    ForEach-Object {
        echo "Datei: $_"  
        $text = Get-Content $_.fullName
        #echo $text
        $IP = $text -match 'IPv4-Adresse|IP-Adresse'  
        $IP = $IP -replace 'IPv4-Adresse.*\:\s|IP-Adresse.*\:\s',''  
        $IP = $IP -replace '\(Bevorzugt\)\s',''  
        $IP = $IP -replace '/s+',''          
        echo "$IP;"  
        $MAC = $text -match '(Physikalische Adresse).*([\d|A-F][\d|A-F]-[\d|A-F][\d|A-F]-[\d|A-F][\d|A-F]-[\d|A-F][\d|A-F]-[\d|A-F][\d|A-F]-[\d|A-F][\d|A-F])'  
        $MAC = $MAC -replace 'Physikalische Adresse.*\:\s',''  
        echo "$MAC;"  
        $HOSTNAME = $text -match "(Hostname)"  
        $HOSTNAME = $HOSTNAME -replace 'Hostname.*\:\s',''  
        $HOSTNAME = $HOSTNAME -replace '/s+',''  
        echo "$HOSTNAME"  
        $info = "$IP;$MAC;$HOSTNAME"  
        $info = $info.TrimStart()
        $info = $info.TrimEnd()
        $info = $info -replace '\s+',''  
        echo $info
        Write-Output $info >> $ziel\test.csv
    }
$stop = Get-Date
$gelaufenezeit = ($stop - $start).TotalSeconds
echo "Auftrag nach $gelaufenezeit Sek. erledigt...."  


gruß michael

ps: danke didi1954... ich möchte es in powershell umsetzen... werde jetzt nach und nach alle meine .vbs sachen in powershell umsetzen.