powershell Powershell RegEx示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell Powershell RegEx示例相关的知识,希望对你有一定的参考价值。

# File: showMyTeamviewerSession.ps1
# Date: 01.03.2013
# Author: Robert Rajakone
# URL: http://tech.robie.ch
# Description: This script extract Teamviewer session connection information from logfile

write-host "Today's Teamviewer Sessions"

# Der Regex-Ausdruck für Extraktion der Daten
$RegexExp = "(\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\s*(\d+)\s*(\d+)\s*([\w\d!]*)\s*(.*$)"



$log="C:\Program Files (x86)\TeamViewer\Version6\TeamViewer6_Logfile.log"
$logdata = gc $log
$logdata -match (get-date -Format "^yyyy\/MM\/dd") -match "(Session to \d+ ended)|(Client connection to \d+)" | ForEach-Object{
    #Write-Host $_
    $extract = [regex]::match($_,$RegexExp)
    $Col1 = $extract.Groups[1].Value  #DateTime
    $Col2 = $extract.Groups[2].Value  #PID
    $Col3 = $extract.Groups[3].Value
    $Col4 = $extract.Groups[4].Value
    $Col5 = $extract.Groups[5].Value  #Comment/Action
    
    
    # Extracting Remote Client ID
    $remoteClient=[regex]::match($Col5, "[Tt]o.(\d{9})").Groups[1].Value
    
    # Converting remoteClient ID to INT and formatting to Teamviewer know ID Format 123 456 789
    $remoteClient=[string]::Format("{0:### ### ###}", [int] $remoteClient)
    
    # Convert Datetime
    $datetime=[datetime]::Parse($Col1)
    
    if ($Col5 -match "ended"){
        write-host $datetime " Disconnecting " $remoteClient
    }else{
        write-host $datetime " Connecting " $remoteClient
    
    }        
}



# Work only in PowerShell Console (NOT IN Powershell ISE!)
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

以上是关于powershell Powershell RegEx示例的主要内容,如果未能解决你的问题,请参考以下文章

SQL 联接查询在 PowerShell 中不起作用

如何在PowerShell中将哈希字符串转换为字节数组?

Windows10 添加shift + 右键打开cmd

Windows密码抓取的四种方式

powershell和powershell ise到底分别干啥用,powershell命令那么长怎么记

powershell怎么运行