我正在尝试清理数百个DNS日志以进行分析。我通过在线研究找到了我正在利用的Powershell代码。但是,代码一次只处理一个文件。我想在同一文件夹中的几个文件上迭代代码。此外,我希望对每个DNS日志的文件名进行子串,以获得服务器名(文件名的前10个字符)。
我使用Get-ChildItem遍历了多个文件,我相信这是成功的。然而,我被困在如何从文件中获取服务器名称并将其放入列中的问题上。
function Get-DNSDebugLog
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$false, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('Fullname')]
[string] $DNSLog = 'StringMode')
BEGIN { }
PROCESS {
$TheReverseRegExString='\(\d\)in-addr\(\d\)arpa\(\d\)'
ReturnDNSLogLines -DNSLog $DNSLog | % {
if ( $_ -match '^\d\d|^\d/\d' -AND $_ -notlike '*EVENT*' -AND $_ -notlike '* Note: *') {
$Date=$null
$Time=$null
$DateTime=$null
$Protocol=$null
$Client=$null
$SendReceive=$null
$QueryType=$null
$RecordType=$null
$Query=$null
$Result=$null
$Date=($_ -split ' ')[0]
$ServerName = $null
# Check log time format and set properties
if ($_ -match ':\d\d AM|:\d\d PM') {
$Time=($_ -split ' ')[1,2] -join ' '
$Protocol=($_ -split ' ')[7]
$Client=($_ -split ' ')[9]
$SendReceive=($_ -split ' ')[8]
$RecordType=(($_ -split ']')[1] -split ' ')[1]
$Query=($_.ToString().Substring(110)) -replace '\s' -replace '\(\d?\d\)','.' -replace '^\.' -replace "\.$"
$Result=(((($_ -split '\[')[1]).ToString().Substring(9)) -split ']')[0] -replace ' '
$ServerName=@{N='First5Chars';E={$_.BaseName.SubString(0,5)}}
}
elseif ($_ -match '^\d\d\d\d\d\d\d\d \d\d:') {
$Date=$Date.Substring(0,4) + '-' + $Date.Substring(4,2) + '-' + $Date.Substring(6,2)
$Time=($_ -split ' ')[1] -join ' '
$Protocol=($_ -split ' ')[6]
$Client=($_ -split ' ')[8]
$SendReceive=($_ -split ' ')[7]
$RecordType=(($_ -split ']')[1] -split ' ')[1]
$Query=($_.ToString().Substring(110)) -replace '\s' -replace '\(\d?\d\)','.' -replace '^\.' -replace "\.$"
$Result=(((($_ -split '\[')[1]).ToString().Substring(9)) -split ']')[0] -replace ' '
$ServerName=@{N='First5Chars';E={$_.BaseName.SubString(0,5)}}
}
else {
$Time=($_ -split ' ')[1]
$Protocol=($_ -split ' ')[6]
$Client=($_ -split ' ')[8]
$SendReceive=($_ -split ' ')[7]
$RecordType=(($_ -split ']')[1] -split ' ')[1]
$Query=($_.ToString().Substring(110)) -replace '\s' -replace '\(\d?\d\)','.' -replace '^\.' -replace "\.$"
$Result=(((($_ -split '\[')[1]).ToString().Substring(9)) -split ']')[0] -replace ' '
$ServerName=@{N='First5Chars';E={$_.BaseName.SubString(0,5)}}
}
$DateTime=Get-Date("$Date $Time") -Format 'yyyy-MM-dd HH:mm:ss'
if ($_ -match $TheReverseRegExString) {
$QueryType='Reverse'
}
else {
$QueryType='Forward'
}
$returnObj = New-Object System.Object
$returnObj | Add-Member -Type NoteProperty -Name Date -Value $DateTime
$returnObj | Add-Member -Type NoteProperty -Name QueryType -Value $QueryType
$returnObj | Add-Member -Type NoteProperty -Name Client -Value $Client
$returnObj | Add-Member -Type NoteProperty -Name SendReceive -Value $SendReceive
$returnObj | Add-Member -Type NoteProperty -Name Protocol -Value $Protocol
$returnObj | Add-Member -Type NoteProperty -Name RecordType -Value $RecordType
$returnObj | Add-Member -Type NoteProperty -Name Query -Value $Query
$returnObj | Add-Member -Type NoteProperty -Name Results -Value $Result
if ($returnObj.Query -ne $null) {
Write-Output $returnObj
}
}
}
}
END { }
}
function ReturnDNSLogLines
{
param(
$DNSLog)
$PathCorrect=try { Test-Path $DNSLog -ErrorAction Stop } catch { $false }
if ($DNSLog -match '^\d\d|^\d/\d' -AND $DNSLog -notlike '*EVENT*' -AND $PathCorrect -ne $true) {
$DNSLog
}
elseif ($PathCorrect -eq $true) {
Get-Content $DNSLog | % { $_ }
}
}
$DNSlogs = Get-ChildItem "C:/users/Desktop/*.log" |
foreach ($d in $DNSlogs):{
$Servername = $DNSlogs[0]}
Get-DNSDebugLog -DNSLog "C:/users/Desktop/*.log" | Export-Csv .\ProperlyFormatedLog2.csv.
发布于 2019-06-07 04:03:23
这不是最好的方法,但这应该可以在不更改函数代码的情况下工作。
$DNSlogs = Get-ChildItem "C:/users/Desktop/*.log"
foreach ($d in $DNSlogs) {
$Servername = $d.basename.substring(0,[math]::Min(10,$d.basename.length))
Get-DNSDebugLog -DNSLog $d.FullName | Select *,@{n='ServerName';e={$ServerName}} | Export-Csv -Path '.\ProperlyFormatedLog2.csv' -NoTypeInformation -Append
}
我知道您说过要将文件名的前10个字符作为服务器名。我确实添加了[Math]::Min()
函数go,如果字符长度小于10,则获取所有字符,以防您的服务器名称较短。文件对象的.basename
属性包含不包括扩展名的文件名。
我觉得可能需要重写才能做你想做的所有事情,其中包括在获得服务器名称的同时进行并行处理。我还看到您的函数设置了$ServerName
,但您从未使用过它。
https://stackoverflow.com/questions/56480407
复制相似问题