यह रेगेक्स मैच

$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

फिर भी मेरी लिपि कभी भी "अन्य" पर नहीं जाती है, यह "अगर" के तहत सबकुछ संसाधित करती है, भले ही कोई मेल न हो।

$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
             }

संपादित करें: एओटी के लिए पूर्ण स्क्रिप्ट

$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
             }


     }       
    }


        }


       }  
0
200mg 23 अप्रैल 2019, 22:39

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
1
AdminOfThings 23 अप्रैल 2019, 19:55