Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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 一个元素包含哈希而不是多个元素-如何修复?_Arrays_Powershell_Hash_Robocopy - Fatal编程技术网

Arrays 一个元素包含哈希而不是多个元素-如何修复?

Arrays 一个元素包含哈希而不是多个元素-如何修复?,arrays,powershell,hash,robocopy,Arrays,Powershell,Hash,Robocopy,我试图解析robocopy日志文件,以修改文件大小、路径和日期。我通过regex获得信息,没有任何问题。然而,出于某种原因,我得到了一个包含单个元素的数组,该元素包含3个哈希。我的术语可能不正确;我还在学习散列。我想要的是一个包含多个元素的规则数组 我得到的输出: FileSize FilePath DateTime --------

我试图解析robocopy日志文件,以修改文件大小、路径和日期。我通过regex获得信息,没有任何问题。然而,出于某种原因,我得到了一个包含单个元素的数组,该元素包含3个哈希。我的术语可能不正确;我还在学习散列。我想要的是一个包含多个元素的规则数组

我得到的输出:

FileSize                         FilePath                         DateTime                        
--------                         --------                         --------                        
{23040, 36864, 27136, 24064...}  {\\server1\folder\Test File R... {2006/03/15 21:08:01, 2010/12...
如您所见,只有一行,但该行包含多个项目。我想要多行

这是我的密码:

[regex]$Match_Regex = "^.{13}\s\d{4}/\d{2}/\d{2}\s\d{2}:\d{2}:\d{2}\s.*$"
[regex]$Replace_Regex = "^\s*([\d\.]*\s{0,1}\w{0,1})\s(\d{4}\/\d{2}\/\d{2}\s\d{2}:\d{2}:\d{2})\s(.*)$"

$MainContent = New-Object System.Collections.Generic.List[PSCustomObject]

Get-Content $Path\$InFile -ReadCount $Batch | ForEach-Object {
    $FileSize = $_ -match $Match_Regex -replace $Replace_Regex,('$1').Trim()
    $DateTime = $_ -match $Match_Regex -replace $Replace_Regex,('$2').Trim()
    $FilePath = $_ -match $Match_Regex -replace $Replace_Regex,('$3').Trim()

    $Props = @{
        FileSize = $FileSize;
        DateTime = $DateTime;
        FilePath = $FilePath
    }
    $Obj = [PSCustomObject]$Props
    $MainContent.Add($Obj)
}

$MainContent | % {
    $_
}
我做错了什么?我就是不明白。谢谢


注意:这需要尽可能快,因为我必须处理数百万行,这就是我尝试System.Collections.Generic.List的原因。

我认为问题是,对于您正在做的事情,实际上需要两个foreach对象循环。将Get Content与-Readcount一起使用将为您提供一个数组。在第一个Foreach对象中使用-Match来过滤每个数组中匹配的记录。这将给你们一个匹配记录的数组。然后需要通过该数组foreach为每个记录创建一个对象:

[regex]$Match_Regex = "^.{13}\s\d{4}/\d{2}/\d{2}\s\d{2}:\d{2}:\d{2}\s.*$"
[regex]$Replace_Regex = "^\s*([\d\.]*\s{0,1}\w{0,1})\s(\d{4}\/\d{2}\/\d{2}\s\d{2}:\d{2}:\d{2})\s(.*)$"

$MainContent = 
  Get-Content $Path\$InFile -ReadCount $Batch |
   ForEach-Object {
    $_ -match $Match_Regex |
     ForEach-Object {
      $FileSize = $_ -replace $Replace_Regex,('$1').Trim()
      $DateTime = $_ -replace $Replace_Regex,('$2').Trim()
      $FilePath = $_ -replace $Replace_Regex,('$3').Trim()

      [PSCustomObject]@{
         FileSize = $FileSize
         DateTime = $DateTime
         FilePath = $FilePath
        }
    }    
 }
您实际上不需要将集合用作累加器,只需输出PSCustomObjects,并让它们在结果变量中累加即可