ユーザ用ツール

サイト用ツール


ログとxmlファイルの整理とリネーム1

文書の過去の版を表示しています。


# .logファイルを取得する
$LogFiles = Get-ChildItem -Filter *.log

# 各.logファイルに対して処理を行う
foreach ($LogFile in $LogFiles) {
    # ファイルの内容を取得する
    $FileContent = Get-Content $LogFile.FullName
    # ホスト名を抽出する
    $Hostname = ($FileContent | Select-String -Pattern "set hostname (.*)").Matches.Groups[1].Value
    # 日付部分を抽出する
    $Date = $LogFile.Name.Substring(0, 8)

    # 出力ファイル名を作成する
    $OutputFileName = "$Date-$Hostname-run.log"
    # 最後のコンフィグの開始行を見つける
    $ConfigStart = ($FileContent | Select-String -Pattern "$Hostname\(.*\)# configure" -AllMatches)[-1].LineNumber
    # コンフィグの終了行を見つける
    $ConfigEnd = ($FileContent[$ConfigStart..($FileContent.Count - 1)] | Select-String -Pattern "$Hostname\(.*\)#" -AllMatches)[0].LineNumber
    # 開始行と終了行の間のテキストを抽出し、出力ファイル名で保存する
    $FileContent[$ConfigStart-1..($ConfigStart + $ConfigEnd-1)] | Set-Content $OutputFileName

    # XMLファイルの処理
    $XmlFile = Get-ChildItem -Filter *.xml | Where-Object { (([xml](Get-Content $_.FullName)).SelectSingleNode("//hostname").InnerText) -eq $Hostname }
    $XmlOutputFileName = "$Date-$Hostname-run.xml"
    # XMLファイル名を変更する
    Rename-Item $XmlFile.FullName $XmlOutputFileName

    # yyyymmddのフォルダを作成し、ファイルをコピーする
    $OutputFolder = New-Item -ItemType Directory -Force -Path $Date
    Copy-Item $OutputFileName -Destination $OutputFolder.FullName
    Copy-Item $XmlOutputFileName -Destination $OutputFolder.FullName
}
ログとxmlファイルの整理とリネーム1.1680676898.txt.gz · 最終更新: 2023/04/05 06:41 by 98.158.246.3

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki