2

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!"
      } 
3
  • This does not appear to be the entire script; you are using variables that are never defined ($ptsl and $ptvf). Please either include the entire script (and tell us the file name), or reduce the script to a minimal standalone script that reproduces the problem. Commented Mar 28, 2017 at 14:34
  • @JeffZeitlin Script is not finished yet. I have edited original post and put everything which I have for now. Commented Mar 28, 2017 at 14:37
  • Your code as shown works for me, but the error you are seeing suggests you are trying to do something recursively beyond the maximum permitted call depth. Is there any recursion in your larger script? Commented Mar 28, 2017 at 14:46

2 Answers 2

2

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).

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

The thing about PowerShell ISE is that it seems to save variables between script executions. If your script references a variable $foo but $foo has not been defined in the current execution, but it was defined in a previous execution, then it will still have the value from that previous execution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.