I am writing a script to read a Status file to record the status of a device. The file follows this style of 'rules': The first line is "[ACTIVE_JOB]" followed by the Jobs listed on each line. Then, there is a line that says "[COMPLETE_JOB]" followed by the jobs listed on each line that are done. At the end of that list, it goes into some other stuff I don't care about, but the first line of the 'other stuff' always starts with a "[". So here's the deal, when I shove the stuff into an Array, and then do a split on the array, if there are NO Active Jobs, it seems to shove the first Completed Job into the Active Jobs array. I'm just trying to make a list or array of the Active Jobs and the Completed Jobs, but alas it is tripping me up when there are no Active Jobs.
Anyways, here is the snippit I am working on (You can uncomment the PATH variable sets to alternate between testing the file reading, if that makes things easier):
#$Path = 'C:\Scripts\NoJOBS.txt'
#$Path = 'C:\Scripts\NoActiveJobs.txt'
#$Path = 'C:\Scripts\ManyJobs.txt'
$StatusFile = Get-Content $path ## Reads the Status File as an array
#$StatusArray = @()
#$ActiveJobs = @()
#$CompleteJobs = @()
$StatusArray = New-Object System.Collections.ArrayList
$ActiveJobs = New-Object System.Collections.ArrayList
$CompleteJobs = New-Object System.Collections.ArrayList
foreach ($Line in $StatusFile) {
if ($Line -like "*PUBLISHER*") {Break}
#$StatusArray += $Line
$StatusArray.Add($Line)
}
$Seperator = "[ACTIVE_JOB]","[COMPLETE_JOB]"
$ActiveJobs,$CompleteJobs = $StatusArray.Split($Seperator,2,[System.StringSplitOptions]::RemoveEmptyEntries)
#$ActiveJobs = [System.Collections.ArrayList]$ActiveJobs
#$CompleteJobs = [System.Collections.ArrayList]$CompleteJobs
foreach ($Job in $ActiveJobs) {
#if($job.Contains("[")){ $ActiveJobs.Remove($Job) }
if($job.Contains("[")){ $ActiveJobs = $ActiveJobs | ? {$_ -ne $job} }
}
foreach ($Job in $CompleteJobs) {
#if($job.Contains("[")){ $CompleteJobs.Remove($Job) }
if($job.Contains("[")){ $CompleteJobs = $CompleteJobs | ? {$_ -ne $Job } }
}
And here is what the file reads like if there are no jobs (Save as NoJOBS.txt):
[ACTIVE_JOB]
[COMPLETE_JOB]
[PUBLISHER1]
NAME=PP-50 1
STACKER1=62
STACKER2=9
STACKER3=0
Here is what it reads like if there is 1 Completed Jobs, but no Active Jobs (Save as NoActiveJobs.txt):
[ACTIVE_JOB]
[COMPLETE_JOB]
JOB1=Frank_L_Smith-085149-08092017
[Frank_L_Smith-085149-08092017]
STATUS=99
And finally, here is what it looks like with 1 active job, and many completed jobs (Save as ManyJobs.txt):
[ACTIVE_JOB]
JOB1=April_Ztest-124856-31082017
[COMPLETE_JOB]
JOB1=Mel_R_Smith-141307-31082017
JOB2=Mel_R_Smithe-132029-31082017
JOB3=Melvin_R_Smithy-131037-31082017
JOB5=Will_R_Smith-123534-31082017
[Mel_R_Smith-141307-31082017]
PUBLISHER=PP-50 1
STATUS=4
PUBLICATION_NUMBER=1
COMPLETION_NUMBER=1
ERRORDISC_NUMBER=0
Using PSVersion 5.1.14393.1066 on Win10 with .NET 4.7