在powershell中的多个函数中使用一个数组

时间:2017-12-14 16:17:37

标签: powershell

我想阅读文本文件的内容并将其添加到数组中。我正在使用Get-Content来执行此操作。

我想知道如何在两个或更多函数中使用此数组。

数组相当大150 MB,我认为为每个函数创建相同的数组效率不高。

function msgList1 ($file){
   $string = "-----------------------------------------------------------------------"
   $record = ""
   $myLine = ""
   $count = 0
   $idxNumber = 0
   $LineNumber = 0
   $count = 0
   $date = @()
   $records = @()


   foreach($line in $file){

        if($line.contains("string of some kind")){
           $myFields = $line.Split(' ')
           [int]$count = $myFields[9] -replace "count="

           While($record -ne $string){
              if($LineNumber -lt 0){break}
              if($count -gt 500){break}
              $LineNumber--
              $record = $file[$LineNumber]
              $count++
           }
           if($count -lt 500 -and $LineNumber -gt 0){
                 $myLine = $file[$LineNumber+1]

                 #Get date and time
                 $dt = $myLine.Split(' ')
                 $date = $dt[1]
                 $time = $dt[2]

                 $newString = $date, $V5time, [string]$count -join " "
                 $records += $newString
                 $record = ""
                 $count = 0
           }
        }
        if($count -ge 500 -or $LineNumber -lt 0){
                  $newString = [string]$count
                  $records += $newString
                  $count = 0
        }

        $LineNumber++
        $idxNumber++

        $LineNumber = $idxNumber
   }

   $records | Out-File "C:\Files\Log Report\Log1Report.txt" -Encoding ascii –Force
}


function msgList2 ($file){
   $string = "-----------------------------------------------------------------------"
   $record = ""
   $myLine = ""
   $count = 0
   $idxNumber = 0
   $LineNumber = 0
   $count = 0
   $date = @()
   $records = @()


   foreach($line in $file){

        if($line.contains("string of some kind")){
           $myFields = $line.Split(' ')
           [int]$count = $myFields[9] -replace "number="

           While($record -ne $string){
              if($LineNumber -lt 0){break}
              if($count -gt 500){break}
              $LineNumber--
              $record = $file[$LineNumber]
              $count++
           }
           if($count -lt 500 -and $LineNumber -gt 0){
                 $myLine = $file[$LineNumber+1]

                 #Get date and time
                 $dt = $myLine.Split(' ')
                 $date = $dt[1]
                 $time = $dt[2]

                 $newString = $date, $V5time, [string]$count -join " "
                 $records += $newString
                 $record = ""
                 $count = 0
           }
        }
        if($count -ge 500 -or $LineNumber -lt 0){
                  $newString = [string]$count
                  $records += $newString
                  $count = 0
        }

        $LineNumber++
        $idxNumber++

        $LineNumber = $idxNumber
   }

   $records | Out-File "C:\Files\Log Report\Log2Report.txt" -Encoding ascii –Force
}

msgList1 -file (Get-Content -Path "C:\Files\Log.txt")
msgList2 -file (Get-Content -Path "C:\Files\Log.txt")

1 个答案:

答案 0 :(得分:0)

您还可以将其设为环境变量:

$env:YourArrayVariable = Get-Content -Path "\\Path\to\your\file"

然后只使用它代替那个$ file