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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 69

Accepted Solution

Qlemo earned 500 total points
ID: 39738019
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

ID: 39738178
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 69

Expert Comment

ID: 39738190
Oh, I haven't spotted that either. Stupid mistake, and so easy to make.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

733 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