[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3525
  • Last Modified:

PowerShell call depth overflowerror

Hello,

I am trying to write a script to parse a field out of several hundred text files.  The script works fine and cranks through as many threads as you toss at it, but eventually I run into the following error:

The script failed due to call depth overflow.  The call depth reached 1011 and the maximum is 1010.
    + CategoryInfo          : InvalidOperation: (1011:Int32) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : CallDepthOverflow

Is there something I can clear in the recursive function to keep it checking frequently without reaching the limit?

Thanks for any assistance.
$ErrorActionPreference = "SilentlyContinue"
$intThreadCount = 10
Get-Job | Stop-Job
Get-Job | Remove-Job
Function fncMTFileParse ($strFullFilePath, $strFileName){
	If((@(Get-Job | ?{$_.State -eq "Running"}).Count) -lt $intThreadCount){
		Start-Job -Name $strFileName -Scriptblock {
			$strFullFilePath = $Args[0]
			$strFileName = $Args[1]
			GC $strFullFilePath | %{
				$_.Split()[4] >> "D:\IndividualResults\$strFileName.ind"
			}
		} -ArgumentList ($strFullFilePath, $strFileName)
	}
	Else{
		Start-Sleep 1
		fncMTFileParse $strFullFilePath $strFileName
	}
}
ForEach($strFile in (GCI .\Logs)){
	fncMTFileParse "$($strFile.FullName)" "$($strFile.Name)"
}
Get-Job | Out-GridView

Open in new window

0
omnipower321
Asked:
omnipower321
1 Solution
 
soostibiCommented:
You called your function in the Else hive recursivel, that is why you received that error message.
I think you would like to do something like this:
$ErrorActionPreference = "SilentlyContinue"  
$intThreadCount = 10  
Get-Job | Stop-Job  
Get-Job | Remove-Job  
Function fncMTFileParse ($strFullFilePath, $strFileName){  
    do{
		Start-Sleep 1
	}until((@(Get-Job | ?{$_.State -eq "Running"}).Count) -lt $intThreadCount)
	Start-Job -Name $strFileName -Scriptblock {  
        $strFullFilePath = $Args[0]  
        $strFileName = $Args[1]  
        GC $strFullFilePath | %{  
            $_.Split()[4] >> "D:\IndividualResults\$strFileName.ind"  
        }
    } -ArgumentList ($strFullFilePath, $strFileName)  
}  
ForEach($strFile in (GCI .\Logs)){  
        fncMTFileParse "$($strFile.FullName)" "$($strFile.Name)"  
}  
Get-Job | Out-GridView

Open in new window

0
 
omnipower321Author Commented:
That worked great.  Thank you  very much!
0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now