Mikhail R
Mikhail R

Reputation: 421

Error "The script failed due to call depth overflow" during execution of PowerShell script

I have a PowerShell script which test if two folders exists and if no folders than create new one. Problem is when I try to execute script I get "The script failed due to call depth overflow" error. Also I would like to put two conditions in one if - if folder 1 and folder 2 don't exist than create folder 1 and folder 2 - else - write folders exists. And (if it's possible) I would like to somehow check - so if folder 1 exists than create only folder 2 and write - folder 1 exists!

# Start script recording

Start-Transcript

# Variable which is path to future/existing PowerShell log folder

$ptsl = "$($env:HOMEDRIVE)\VL\Win_mult_machines"

# Variable which is path of future/existing Vagrant folder

$ptvf = "$($env:HOMEDRIVE)\VM\Win_mult_machines"

# Variable which is future/existing name of log and Vagrant folders

$lvn = "Win_mult_machines"

# Test path for script log - if path doesn't exist - create it

if (-Not (Test-Path $ptsl -PathType Container))

      {
       New-Item -Path "$($env:HOMEDRIVE)\VL\" -Name $lvn -ItemType Directory      
      }

 else {
         Write-Output "Folder already exists!"
      }

# Test path for Vagrant box folder - if path doesn't exist - create it

 if (-Not (Test-Path $ptvf -PathType Container))

      {
       New-Item -Path "$($env:HOMEDRIVE)VB\" -Name $lvn -ItemType Directory
      }

 else {
       Write-Output "Folder already exists!"
      } 

Upvotes: 2

Views: 20068

Answers (2)

Mikhail R
Mikhail R

Reputation: 421

This is weird PowerShell error - I have reloaded PowerShell ISE and issue is gone.

Upvotes: 1

Mark Wragg
Mark Wragg

Reputation: 23385

I'm not sure if this is causing your issue, but there is a typo in your code:

   New-Item -Path "$($env:HOMEDRIVE)VB\" -Name $lvn -ItemType Directory

Should be:

   New-Item -Path "$($env:HOMEDRIVE)\VB\" -Name $lvn -ItemType Directory

The error you are seeing suggests you are trying to do something recursively beyond the maximum permitted call depth (e.g new-item is calling itself more than 100 times).

Upvotes: 2

Related Questions