PowerShell

Tipps zur Verwendung der Microsoft PowerShell, nĂĽtzliche Befehle und Skripte

  • Korn Shell / Bash,  PowerShell,  Shell Scripting,  Windows Bash

    đź’ˇHow to Extract Specific Lines from a File: sed, awk, head/tail, grep, and with powershell

    In the command line, it’s often necessary to extract only specific lines from a text file – whether it’s a single line or a range of lines. Fortunately, Bash offers several powerful tools for this. In this blog post, I’ll show you the most common methods using sed, awk, head/tail, and grep. Additionally, I will demonstrate an option when using PowerShell. Extracting Lines with sed sed (Stream EDitor) is a classic for text manipulation in the shell and is excellent for selectively outputting line ranges.Output a Single Line (e.g., Line 102)To output a specific line, such as line 102, you use the p command (print) in combination with the -n…

  • PowerShell,  Shell Scripting,  Uncategorized

    đź’ˇPowershell Tip – Search directories and subdirectories for files with specific content

    I’d like to share a small PowerShell script that scans all subdirectories for specific files (for example, *.yaml and *.yml) and searches them for a specific string (e.g., “ansible.builtin.template”). The result is returned with the location (name and path of the file, and line number of the search string in the file). What it does: Sometimes I want to save my results to a file and explore them further. To save the results to a CSV file, adapt the script as follows:

  • Middleware,  PowerShell

    Umbenennung von Dateien per powershell

    In Linux Umgebungen haben Administratoren fĂĽr Logdateien von WebSphere Application Server, Tomcat und andere Middleware Produkte meist Skripte, um die Protokolle täglich oder wöchentlich umzubenennen (per crontab). Unter Windows habe ich auf die schnelle keine vernĂĽnftige Scriptlösung gefunden. Meine Lösung stelle ich in meinem Azure DevOps Projekt pwshLogSwitch zur VerfĂĽgung. Das kleine Script erwartet mindestens ein Startverzeichnis als Parameter. Alle Dateien in diesem Verzeichnis und darunter werden umbenannt, solange die Kriterien passen. Die Auswahlkriterien werden mit den Parametern -Filter -IncludePattern -ExcludePattern definiert und entsprechen den gleichnamigen Parametern von Get-ChildItem. Es können auch reguläre AusdrĂĽcke und Wildcards ĂĽbergeben werden. Als Verzeichnis können mehrere Verzeichnisnamen per : getrennt ĂĽbergeben werden. IncludePattern und…

  • PowerShell

    đź’ˇPowershell Tip: Tail und Head vereint

    Ausgabe der letzten und der ersten n-Zeilen in einemBefehl In Unix/Linux Derivaten sind die Befehle tail und Head oft benutzte Werkzeuge zur Ausgabe der ersten/letzten Zeilen einer Datei oder der Ergebnisse eines Befehls. Direkt kombinieren kann man sie nicht. Hier sind sed/awk und andere Werkzeuge und Lösungen gefordert. Powershell bietet eine sehr effiziente Lösung. Der erste Ansatz wäre die Ausgabe mit Select -Last und Select -First zu behandeln. Das geht aber nicht in einem Befehl (oder nur mit einer zu schreibenden Funktion). Damit haben wir das gleiche Problem wie mit head und tail in Unix. Per Get-ChildItem und einem auf die Ausgabe angewendetem Sort Befehl erhält man eine Liste. Diese…

  • PowerShell,  Windows Bash,  WSL

    đź’ˇPowerShell Tip for Large Files – The Get-Content -Tail and -Wait parameters

    In PowerShell, you can display the last *n* lines of large text or ASCII files using the `-Tail` parameter of the `Get-Content` cmdlet: In Powershell, you can display the last *n* lines of large tyt or ASCII files using the -Tail parameter of the Get-Content cmdlet: Since PowerShell version 3, Get-Content (or its alias gc) includes the -Tail parameter. Unfortunately, there is no built-in equivalent to the Unix tail -f command for continuous updates. PowerShell: Get-Content with -Wait PowerShell’s Get-Content cmdlet supports the -Wait parameter, which continuously monitors a file for new content: This will display new lines as they are appended to the file. PowerShell with filtering (advanced usage)…

  • PowerShell,  Windows

    PowerShell Drive alias:

    PowerShell nutzt das Konzept der Laufwerke (=PowerShell Drives) sehr ausgiebig. So werden die Umgebungsvariablen in env: verwaltet, die Windows Registry aber auch die “normalen” Laufwerke wie “C:”. Auch Aliasse fĂĽr Kommandos sind ein PowerShell Drive sind. Get-ChildItem alias:

  • PowerShell,  Windows

    Mehrfache Einträge in großen Konfigurationsdateien oder Protokollen finden (Windows Powershell Lösung)

    Im Post Mehrfache Einträge in groĂźen Konfigurationsdateien oder Protokollen finden(Linux/Unix) habe ich gezeigt, wie mehrfach vorkommende Zeilen in Textdateien gefunden und automatisch entfernt werden können. Windows hat im Zuge der Powershell ein mächtiges Werkzeug, dass dies auch kann. (Seit dem Windows 10 1709 Update kann man natĂĽrlich auch einfach die eingebaute bash nehmen….) Zur Demonstration verwende ich eine Datei duplicates.txt: Get-Content -Path duplicates.txt Zeile kommt nur einmal vor Zeile kommt zweimal vor Zeile kommt dreimal vor Zeile kommt zweimal vor Zeile kommt dreimal vor Zeile kommt auch nur einmal vor Zeile kommt dreimal vor Mehrfache vorkommende identische Zeilen und deren Anzahl findet und zählt man wie mit Group-Object Get-Content -Path…