Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 如何从多个文件中提取特定图案线并获取文件名&;每行的修改日期-Powershell_Arrays_Powershell_Powershell Ise - Fatal编程技术网

Arrays 如何从多个文件中提取特定图案线并获取文件名&;每行的修改日期-Powershell

Arrays 如何从多个文件中提取特定图案线并获取文件名&;每行的修改日期-Powershell,arrays,powershell,powershell-ise,Arrays,Powershell,Powershell Ise,流程摘要: #Creating the String, and applying the filter to only take the 'Applying' lines, and then creating a new line for it to repeat. $myString = (Select-String -Path "C:\FilePathHere\*.log" -Pattern '^Applying ').Line # This will take the Modified

流程摘要:

#Creating the String, and applying the filter to only take the 'Applying' lines, and then creating a new line for it to repeat.
$myString = (Select-String -Path "C:\FilePathHere\*.log" -Pattern '^Applying ').Line

# This will take the Modified date from the File.
$lastModifiedDate = (Get-Item "C:\FilePathHere\*.log").LastWriteTime

$arr = $myString -split ' '

$Groups1 = @()
$Groups = @()

#Create a table holding just the specific info from each line required, and putting that into a table.

foreach($members in $myString)
        {
            $arr = $members -split ' '

            $Groups1 = New-Object PSObject 
            $Groups1 | Add-Member -type NoteProperty -Name 'Object Updated' -Value $arr[1]
            $Groups1 | Add-Member -type NoteProperty -Name 'New Version' -Value $arr[3]
            $Groups1 | Add-Member -type NoteProperty -Name 'Old Version' -Value $arr[8]
            $Groups1 | Add-Member -type NoteProperty -Name 'Server/DB' -Value $arr[5]
            $Groups1 | Add-Member -type NoteProperty -Name 'Updated Date' -Value $lastModifiedDate
            $Groups += $Groups1  
        }  
# to display the recorded and ordered info:
$Groups

有一个修补过程,创建的日志文件全部进入1个文件夹,不会再次更新或修改。所有修补程序中的所有日志文件都位于此处,不会被覆盖


在此修补过程中,它会对多个区域执行多个更新,但只会更新版本低于新更新的部件。(例如,'Section.X'的版本是12,修补程序是将Section.X 15设置为您定义$lastModifiedDate并将其设置为'Updated Date'的值的方式会导致变量永远不会更改,而不管$myString来自哪个日志文件。我会逐个查看每个文件,以便每个文件的LastWriteTime保持为m。)y表已创建。我将尝试以下操作:

$logfiles = Get-ChildItem -Path C:\FilePathHere\*.log

$Groups1 = @()
$Groups = @()

foreach ($logfile in $logfiles) {

    $myString = (Select-String -Path "C:\FilePathHere\$($logfile.Name)" -Pattern '^Applying ').Line

    $arr = $myString -split ' '

    $lastModifiedDate = $logfile.LastWriteTime

    foreach ($members in $myString) {

        $arr = $members -split ' '

        $Groups1 = New-Object PSObject 
        $Groups1 | Add-Member -type NoteProperty -Name 'Object Updated' -Value $arr[1]
        $Groups1 | Add-Member -type NoteProperty -Name 'New Version' -Value $arr[3]
        $Groups1 | Add-Member -type NoteProperty -Name 'Old Version' -Value $arr[8]
        $Groups1 | Add-Member -type NoteProperty -Name 'Server/DB' -Value $arr[5]
        $Groups1 | Add-Member -type NoteProperty -Name 'Updated Date' -Value $lastModifiedDate
        $Groups += $Groups1  

    }

}

# to display the recorded and ordered info:
$Groups

我将利用
Select String
cmdlet上的
-Path
参数来缩短循环,并将PSObject数组输出到变量:

$result = Select-String -Path 'C:\FilePathHere\*.log' -Pattern '^Applying ' | ForEach-Object {
    $arr = $_.Line -split ' '
    [PsCustomObject]@{
        'Object Updated' = $arr[1]
        'New Version'    = $arr[3]
        'Old Version'    = $arr[8]
        'Server/DB'      = $arr[5]
        'LogFile'        = $_.Path
        'Updated Date'   = (Get-Item -Path $_.Path).LastWriteTime
    }
}

# output on screen
$result

# output to CSV
$result | Export-Csv -Path "D:\logInfo.csv" -NoTypeInformation

希望对您有所帮助

请您从日志文件中发布几行内容,并显示您当前的代码从中得到了什么…以及哪些内容不符合预期?除非您的文件具有显示其时间戳的模式,否则您无法动态地计算出一行内容是何时添加到文件中的。使用
Get ChildItem
可能会更幸运>获取项
以查找您的文件。然后
foreach对象
foreach
这些文件中的每一个。如果您调整当前对象(文件)的
选择字符串
,则可以包装此代码。它可能效率较低,但为了实现目标,您可能不得不牺牲简洁性和效率。在您的代码中,
$lastModifiedDate=(获取项“C:\FilePathHere\*.log”).LastWriteTime
将在返回多个文件时存储一个日期时间数组;这是您的错误。如果希望此上下文在逻辑上正确,您应该在解析过程中同时迭代这些文件。这是一笔巨大的财富,我在另外一行中添加了一行,以便还返回每行的文件名(它来自何处)。感谢所有的帮助!所有对此发表评论的人都会得到帮助。由于您正在使用
Get ChildItem
,循环中的每个
$logfile
都已经是一个FileInfo对象。要获取LastWriteTime属性,您不需要(重新设置)
获取项目
,只需使用
$logfile.LastWriteTime
。确实如此。我主要是复制粘贴的内容,但没有捕捉到。它已编辑。谢谢。