I'm trying to simplify the situation as much as possible.
I have a set of files in that needs to be processed in a certain order because some of them are dependant on each other. But I don't have any reliable means to find out if a file has its dependencies fullfilled before processing it. - What i do have is an external function that throws an error if I try to process a file too early. Thats why I'm trying to iterate though those files until all of them have been processed.
(those files contain so called "extensions" if you wonder about the variable names.)
What I'm trying to do is, catching the Files that are not able to get published to the server, yet in the "catch" area and start the while loop over with the remaining set until all files are processed or the script reached its deadloop limiter of 20 loops.
$path = 'C:\abcd'
$ExtList = (Get-ChildItem $path -Filter '*.app' -Recurse).FullName
$i = 0 #stop deadloop
while (($ExtList.Count -gt 0) -and ($i -lt 20)) {
$i++
[array]$global:FailedExt = @()
[array]$global:FailedExtError = @()
$ExtList | % {
try {
Publish-Extension -Path $_
} catch {
$global:FailedExt += $_
$global:FailedExtError += $Error[0]
}
}
#Set ExtList to list of remaining Extensions
$ExtList = $global:FailedExt
}
if ($global:FailedExt.Count -gt 0) { #dependencies weren't the Problem
$ErrorMsg = "Could not publish the following Extensions:`n`n"
for ($i = 0; $i -lt $FailedExt.Count; $i++) {
$ErrorMsg += "Error in: " + ($global:FailedExt[$i]) + ":`n"
$ErrorMsg += " - Error: " + ($global:FailedExtError[$i]) + "`n`n"
}
throw $ErrorMsg
}
MY Problem:
Apparently the value of $_ in the catch-block isn't the same as in the try-block because i get
This error output:
Could not publish the following Extensions:
Error in: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Illegal characters in path.":
- Error: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Illegal characters in path."
Error in: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Illegal characters in path.":
- Error: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Illegal characters in path."
Error in: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Illegal characters in path.":
- Error: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Illegal characters in path."
And then, on the second iteration, the script tries to use the Error-Message as a Path to the file I'm trying to publish. - Which results in a set of "illegal characters in path"-Messages.
p.s. If you know any other improvements to my code, let me know (I'm new to PS)