正则表达式匹配不会产生预期的结果

时间:2019-04-23 19:39:55

标签: powershell

此正则表达式匹配项

$script:lstfilepath = ((Get-Content -path $script:ordfile) | Select-String -pattern "^file\s*=\s*(\\\\.*.lst)").matches.groups[1].value

在不包含匹配项的文件上产生此错误,让我相信返回的结果为空。

Cannot index into a null array.
At line:1 char:1
+ ((Get-Content -path .\27257055-brtcop.ORD) | Select-String -pattern " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray

但是我的脚本从不跳到“其他”,即使没有匹配,它也会处理“ if”下的所有内容。

$script:lstfilepath = ((Get-Content -path $script:ordfile) | Select-String -pattern "^file\s*=\s*(\\\\.*.lst)").matches.groups[1].value
        if (-not ([string]::IsNullOrEmpty($script:lstfilepath)))
         {
          ###LST PROCESS
          Write-Host "LST FILE PRESENT"
          $script:lstpayload = Get-Content $script:lstfilepath |ForEach-Object { ($_ -split '"')[-2] }
           FOREACH ($script:lstfile in $script:lstpayload)
            {
              $script:lstzipshortname = (-join ((48..57) + (97..122) |get-random -count 11 |% {[char]$_}))
              $script:lstzipname = $script:lstzipshortname + ".zip"
              7z a -spf  $script:lstzipname $script:lstfile
            }
         Set-Location $global:ordprocessingpath
         copy-item -Recurse $script:ordprocfolder c:\temp 
         }
         else   
            { 
             ###REGULAR PROCESS
             $script:filepath = ((Get-Content -path $script:ordfile) | Select-String -pattern "^file\s*=\s*(\\\\.*\\)").matches.groups[1].value
             $script:zipshortname = $script:ordfile
             $script:zipname = $script:zipshortname + ".zip"
             7z a -spf $script:zipname $script:filepath
             }

编辑:AoT的完整脚本

$global:ordrepopath = "C:\test_environment\ORD_REPO"
$env:path = "c:\program files\7-zip"
$global:datestr = (Get-Date).ToString("MMddyyyyhhmmss")
$global:ordlogpath = "C:\test_environment\ORD_REPO\ORD_LOGS\"
$global:ordlogcheck = "C:\test_environment\ORD_REPO\ORD_LOGS\*.log"
$global:ordlogstagingpath = "C:\test_environment\ORD_REPO\ORD_STAGING"
$global:ordlogarchivepath = "C:\test_environment\ORD_REPO\ORD_LOG_ARCHIVE"
$global:ordprocessingpath = "C:\test_environment\ORD_REPO\ORD_PROCESSING"
$global:copypath = "C:\test_environment_2\share\STAGING\PRE_STAGING"
$global:ordlogdestpath = "C:\test_environment_2\Share\Staging\Pre_staging\processed_logs"

###DEFINE LOG FILE
$script:scriptlogfile = "C:\test_environment\ORD_REPO\SCRIPT_LOGS\ORD_PROCESS_LOG_$(get-date -format `"yyyyMMdd_hhmmss`").log"
Start-Transcript -Path $script:scriptlogfile -NoClobber

 if (!(Test-Path -Path $global:ordlogcheck)) 
  {
   Write-Host "NO FILES TO PROCESS"
  }
 else
  {
  ### CREATE ARCHIVE DIRECTORY
  New-Item -Path "C:\test_environment\ORD_REPO\Archive\$global:datestr" -ItemType Directory
  $script:archivepath = "C:\test_environment\ORD_REPO\Archive\$global:datestr"
  $script:ordlogfiles = Get-ChildItem -Path $global:ordlogpath -File
   ForEach ($script:ordlogfile in $script:ordlogfiles)
   {
    Set-Location $global:ordlogpath
    $script:ordlogimport = (Import-Csv $script:ordlogfile.FullName).file |sort
    $script:ordprocfoldername = ($script:ordlogimport |Select-Object -First 1)
    New-Item -Path "C:\test_environment\ORD_REPO\ORD_PROCESSING\$script:ordprocfoldername" -ItemType Directory
    $script:ordprocfolder = "C:\test_environment\ORD_REPO\ORD_PROCESSING\$script:ordprocfoldername"
    Set-Location $global:ordrepopath
    $script:ordlogimport |ForEach-Object {move-item $_ $script:ordprocfolder}
    Set-Location $global:ordlogpath
    copy-item $script:ordlogfile $script:archivepath
    move-item $script:ordlogfile $script:ordprocfolder
    Set-Location $script:ordprocfolder
    $script:ordfiles = Get-ChildItem $script:ordprocfolder -Include *.ord,*.nwp | ForEach-Object { $_.Name }
    FOREACH ($script:ordfile in $script:ordfiles)
     {
      $script:ordlogcount = ($script:ordlogimport).count
      $script:ordcount = ((get-childitem $script:ordprocfolder).count -1)
      if ($script:ordlogcount -ne $script:ordcount)
       {
        WRITE-HOST "MISSING ORD FILE"
       }
      else
       {
        $script:lstfilepath = ((Get-Content -path $script:ordfile) | Select-String -pattern "^file\s*=\s*(\\\\.*.lst)").matches.groups[1].value
        if (-not ([string]::IsNullOrEmpty($script:lstfilepath)))
         {
          ###LST PROCESS
          Write-Host "LST FILE PRESENT"
          $script:lstpayload = Get-Content $script:lstfilepath |ForEach-Object { ($_ -split '"')[-2] }
           FOREACH ($script:lstfile in $script:lstpayload)
            {
              $script:lstzipshortname = (-join ((48..57) + (97..122) |get-random -count 11 |% {[char]$_}))
              $script:lstzipname = $script:lstzipshortname + ".zip"
              7z a -spf  $script:lstzipname $script:lstfile
            }
         Set-Location $global:ordprocessingpath
         copy-item -Recurse $script:ordprocfolder c:\temp 
         }
         else   
            { 
             ###REGULAR PROCESS
             $script:filepath = ((Get-Content -path $script:ordfile) | Select-String -pattern "^file\s*=\s*(\\\\.*\\)").matches.groups[1].value
             $script:zipshortname = $script:ordfile
             $script:zipname = $script:zipshortname + ".zip"
             7z a -spf $script:zipname $script:filepath
             }


     }       
    }


        }


       }  

1 个答案:

答案 0 :(得分:1)

如果$script:lstfilepath具有先前的值,则Select-String产生的错误将不会覆盖该值。因此,您需要在执行此代码之前清除$script:lstfilepath的内容,或再次声明它。下面是您的问题的副本。

$g = "hi"
$g = (select-string -pattern "h(h)" -InputObject "tree").matches.groups[1].value
Cannot index into a null array.
At line:1 char:1
+ $g = (select-string -pattern "h(h)" -InputObject "tree").matches.grou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
-not ([string]::IsNullOrEmpty($g))
True

$g
hi

这是使用Clear-Variable解决该问题的一种方法:

$g
hi

Clear-Variable g
$g = (select-string -pattern "h(h)" -InputObject "tree").matches.groups[1].value
Cannot index into a null array.
At line:1 char:1
+ $g = (select-string -pattern "h(h)" -InputObject "tree").matches.grou ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray
-not ([string]::IsNullOrEmpty($g))
False