Getting results out of nested loops

by OliAdams at 2013-03-24 16:20:05

I think I am getting more confused the more I try with this. I have a text block and go through it line by line and save matches, but how do i get the data out without saving it to an external file first.

$Command = ‘C:\Program Files\Symantec\Backup Exec\bemcmd’
$JobType = ‘-o12’
$Arg = ‘-i’

$JobIds = & $Command $JobType $Arg

ForEach ($ID in $JobIds){

IF ($ID){

IF($ID -match ‘JOB ID’){

$Code = $ID.Substring($ID.Indexof(’{’),($ID.Length - ($ID.Indexof(’{’))))

$Code | Out-File e:\IDS.txt -Append

}

}

}

$Jobids = Get-Content -Path E:\IDS.txt | Select-Object -Unique

$JobIds


This is the code I have now which does work I just feel like I should be doing this differently.
by DonJ at 2013-03-24 18:10:10
So, you’ve got a couple of ways. The easy way is to probably just construct a blank array:

$Output = @()

At the top of your script, and then add to it:

$Output += $code

Instead of piping it to a file. There’s a more elegant technique that would involve reconstructing your code as a function and having it output to the pipeline, but I’m not sure what your final use for this is, and it does get into a bit of an architectural discussion.
by OliAdams at 2013-03-25 00:32:39
Ok great. As in a final use for the script the list of job numbers is parsed and unique values are retrieved. Then for each unique job number a list of backups is retrieved. Then for each backup the job information is extracted. The script works I just want to learn how I should of done it so I can make the script as efficient as possible. I will modify the script to use arrays and after is it allowed to post a whole script to get feedback? Thanks for your fast reply.
by DonJ at 2013-03-25 06:28:04
You can post a script and ask for feedback; I can’t promise is anyone will offer any :).