powershell:コンフィグファイル抜きだし_名前整理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 }
カレントディレクトリにある.logファイルを取得します。 各.logファイルに対して、ホスト名を抽出し、日付部分を取得します。 出力ファイル名をyyyymmdd-ホスト名-run.log形式で作成します。 ファイル内の最後のホスト名()# configureから、その次のホスト名()#までのテキストを抽出し、出力ファイル名で保存します。 カレントディレクトリにある.xmlファイルを取得し、.logファイルと同じホスト名を持つ.xmlファイルを見つけます。 .xmlファイルのファイル名をyyyymmdd-ホスト名-run.xml形式に変更します。 yyyymmdd形式のフォルダを作成し、その中に変更された.logファイルと.xmlファイルをコピーします。
powershell/コンフィグファイル抜きだし_名前整理1.txt · 最終更新: 2023/04/05 23:21 by taatin