Это совпадение с регулярным выражением
$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
Тем не менее, мой сценарий никогда не переходит к «else», он обрабатывает все под «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
}
}
}
}
}