PS 3 variable scope issue inside FOR loop

Posted on 2013-12-24
Last Modified: 2013-12-24
In the code snippet below, I cannot set the E3Path while inside the FOR loop, but can set it outside of the loop without any problem. I've tried prefixing "script:" , but it made no difference:
   script:$CSVRecord.E3Path = "Popcorn"

I know, or think, that items allocated in the FOR loop are local, but I thought I would be able to access variables outside of it with no problem.

Please explain and how to solve.

$CSV = Invoke-Sqlcmd -Query $Query

ForEach ($CSVRecord in $CSV)
#find the current record number being processed
 [int]$currentrecord = [array]::IndexOf($CSV,$CSVRecord)

if ($deleteflag=1)
            write-host ***********************************************
            Write-Host Current Record# $currentrecord
            #skip the 1st record
 #THIS WORKS AS EXPECTED "Popcorn3" is set in the record
           $CSVRecord.E3Path = "Popcorn3"

            $filecount = $CSVRecord.filecount-1
            for ($i=0; $i -lt $filecount; $i++)
            $CSVRecord.E3Path = "Popcorn"
            write-host ***********************************************

Question by:johnj_01201
  • 2
LVL 68

Accepted Solution

Qlemo earned 500 total points
Comment Utility
The proper syntax is: $script:CSVRecord.E3Path = "Popcorn", but it should work without. Unless you create a new var with the same name in the FOR, which you don't.

I reckon, since the code you showed doesn't make much sense, that your real code contains something causing that effect.

Author Comment

Comment Utility
OK thanks. The problem was the IF statement was $deleteflag=1 instead of $deleteflag -eq 1, so the code was never being run and I used the same block of code in another part of the program, so I thought it was actually running here. I moved it here to troubleshoot.
LVL 68

Expert Comment

Comment Utility
Oh, I haven't spotted that either. Stupid mistake, and so easy to make.

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now