Solved

PS 3 variable scope issue inside FOR loop

Posted on 2013-12-24
3
278 Views
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.
thanks



$CSV = Invoke-Sqlcmd -Query $Query

$deleteflag=0
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++)
            {
#WHY DOESN'T THIS NEXT LINE DO ANYTING?
            $CSVRecord.E3Path = "Popcorn"
            }
            write-host ***********************************************
$deleteflag=0

pause
$CSVRecord
pause
}
0
Comment
Question by:johnj_01201
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
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.
0
 
LVL 1

Author Comment

by:johnj_01201
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.
0
 
LVL 68

Expert Comment

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

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

932 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

12 Experts available now in Live!

Get 1:1 Help Now