VMware Cloud Community
mittim12
Immortal
Immortal

File Creation Process

I have a script that executes three separate code blocks. The first block performs a query and then creates a text file with the results. The second block performs a second query and then attempts to append it's results to the text file originally created in the first block. The second block ends up failing because a process in the first block still holds the file open. What is proper way to close out this connection before moving onto the next code block? I am using out-file. Thanks for any help that is given.

0 Kudos
6 Replies
LucD
Leadership
Leadership

The Out-File cmdlet normally takes care of the opening and closing of the file.

Could you perhaps include the code block skeletons (with the Out-File cmdlets) ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
mittim12
Immortal
Immortal

The initial creation of the file is shown below, and the second block is the same format as the first just with the -append option.

Get-VM | Get-Snapshot | select VM, Name, Created | Out-File c:\scripts\scriptlog\vmwarereport.txt

0 Kudos
DougBaer
Commander
Commander

This works fine for me, even if I put the out-file commands on the same line

get-vm | out-file test.txt ; get-vmhost | Out-File -append test.txt

Maybe something like a virus scanner is holding the file open longer than usual?

Doug Baer, Solution Architect, Advanced Services, Broadcom | VCDX #019, vExpert 2012-23
LucD
Leadership
Leadership

Same here, works for me without a problem.

From where do you run the script ? PS prompt, PowerCLI prompt, a GUI...

What is the exact message you get ?

What version of PowerShell and the PowerCLI are you using ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

mittim12
Immortal
Immortal

I was running it through PowerGui and after running process monitor it looked as if PowerGui wasn't releasing the file. I ran it the regular way and everything worked as it should. Thanks for the help.

0 Kudos
dpenderg
Contributor
Contributor

PowerGUI is not working as intended Smiley Sad . Would you mind posting the bug and your script text on the PowerGUI.org forums so we can fix the problem?

0 Kudos