Problem Using Get-Content Cmdlet

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
bill-douse
Posts: 7
Joined: Thu Oct 08, 2020 3:08 pm

Problem Using Get-Content Cmdlet

Post by bill-douse »

Can someone help me understand why the following three lines in a PS Studio script do not work. But, they do work when run in PS ISE or as a PS command-line?

$TextFile = "C:\TextList.txt" #There are 85 lines of text in this file
$FileTextLines = Get-Content -Path $TextFile
$LineCT = $FileTextLines.Count

When I run this script in PS Studio $LineCT returns 0; whereas, when I run it in PS ISE it returns 85. Also, when I mouse over the $FileTextLines variable during a PS Studio debug session it doesn't show any data in the object at all.

I also noticed that when I use the Readline() as an alternative approach to the problem I get an empty string back, as well. But, when I step through the Readline() loop in a debug session it will read and populate the variable properly. Only when executed without stopping it returns an empty string. Strange.

Appreciate any help with this issue. This is a day-old install of the newly purchase PS Studio. Could this be a bug in PS Studio, or are the execution environments different between PS Studio and PS ISE?

User avatar
Alexander Riedel
Posts: 7564
Joined: Tue May 29, 2007 4:43 pm
Answers: 2
Been upvoted: 5 times

Re: Problem Using Get-Content Cmdlet

Post by Alexander Riedel »

I think I know what the issue is here. You are expecting the values to be shown in the editor after you ran the script. Like in the ISE.
We don't do that. Each and every time you run a script it is in its own process and runspace and that gets terminated after the script has executed.
Otherwise you get what is called runspace contamination, which is a major cause of errors.
Normally you would just output he values to see them during execution if you need to.
You can also use "Run in console" in PowerShell Studio if you really need to keep the runspace for further experimentation. Then you can examine values after the fact in the console.

Additionally, in the future please post product questions in the corresponding product forum with the version information requested.
Alexander Riedel
SAPIEN Technologies, Inc.

bill-douse
Posts: 7
Joined: Thu Oct 08, 2020 3:08 pm

Re: Problem Using Get-Content Cmdlet

Post by bill-douse »

Sir,

Sorry about the lack of version info. I had run into an issue posting this message the first time due to a URL screening error and Customer Service helped me out. But, when I reposted it I totally didn't notice that the product information was left out of the cut and paste. This is the header information that was in the original message:

PowerShell Studio 2020 (64 Bit)
Build: v5.7.181
OS: Windows 10 Pro (64 Bit)
Build: v10.0.18363.0
Powershell Version: 5.1.18362.752

Now, about your response. I am not sure I understand what you are trying to articulate. I am running those three lines in a Function in a rather large PS Studio compiled program, and they do not work as expected. During execution of the script the Get-Content cmdlet fails to read the text file into the variable, but there is no error/exception shown in the Output Console. It just does not work. I should be able to see the cmdlet results in a debug session following the execution of that Get-Content cmdlet line, but it just shows an empty object. I don't think it is a PS Studio problem, per say, because I get the same results when I run it in VC Code. I just don't understand why these three lines do work in an ISE script, but not PS Studio or VC Code. Then again, I am not a Powershell expert by any means. Just trying to figure out if I am doing something wrong in the script (e.g., declaration problem, environmental setting issue, etc.)

Thanks,
Bill D.

User avatar
Alexander Riedel
Posts: 7564
Joined: Tue May 29, 2007 4:43 pm
Answers: 2
Been upvoted: 5 times

Re: Problem Using Get-Content Cmdlet

Post by Alexander Riedel »

Ah, the plot thickens. Are you running the ISE elevated? I think that is still the default. If you are not running elevated you cannot open or modify file in the root folder.
It is a protected folder in modern Windows versions.
Generally, unless it is absolutely necessary, you should never place any data files in the root of C:\ (or any other drive).
Alexander Riedel
SAPIEN Technologies, Inc.

bill-douse
Posts: 7
Joined: Thu Oct 08, 2020 3:08 pm

Re: Problem Using Get-Content Cmdlet

Post by bill-douse »

Yes. I do run PS Studio, ISE, and VC Code using Runas Administrator. I also have the statement #%ForceElevation% = yes declared in the script. The file is located in a subfolder called C:\DCA-ABT\Textfile.txt

User avatar
Alexander Riedel
Posts: 7564
Joined: Tue May 29, 2007 4:43 pm
Answers: 2
Been upvoted: 5 times

Re: Problem Using Get-Content Cmdlet

Post by Alexander Riedel »

Ok, so we are having some mixed messages here. Your first post puts the file in the root folder. Then you mention a packaged script, for which I cannot be sure what options are you selected.
It is also impossible to understand which exact code in what particular circumstance now works or not. So let's reset this.
Put the minimal amount of code in a stand alone script. Execute it in PowerShell Studio and describe what happens. Then attach the script here.
Last but not least, you should never have to run PowerShell Studio elevated. Running any process all day elevated is genuinely a security risk. If you need to run a script elevated, use the elevation toggle switch on the ribbon.
Alexander Riedel
SAPIEN Technologies, Inc.

bill-douse
Posts: 7
Joined: Thu Oct 08, 2020 3:08 pm

Re: Problem Using Get-Content Cmdlet

Post by bill-douse »

Okay. I finally figured out what was happening. It was one of those bugs that are hard to catch without extensive debugging efforts. Your recommendation to conduct a separate test in a new PS Studio script helped lead me to the solution. The text file I was trying to read was actually being created at the same that the Get-Content cmdlet was trying to read it. This is because the text file is created using a Start-Process cmdlet in the same function and I did not include the -Wait parameter. Another indicator that pointed me in the right direction was using a StreamReader approach, which also worked fine in a debug session, but not during an actual run. But, at least during the run the StreamReader threw a "file in use by another process" exception, which led me back to the Start-Process cmdlet. That begs the question, why does the Get-Content cmdlet not throw a similar exception? Would have saved me hours of frustration.

Simple omissions like this can result in days of troubleshooting. That is, unless a more disciplined debugging approached is used. LOL.

Thanks-a-million for your help in guiding me to the problem.

Regards,
BiIl D.

Locked