0

I'm making a script for publishing web applications with PowerShell.

Since I'm early in development I edit the script and the xml file alternately. Right now, the script is quite small and simple:

$settings = [xml](Get-Content c:\PublishData.xml)
$whereToProjectsAreLocally = $settings.publishing.globals.localprojectdir

But I do have a problem. Once I launch the script and check that everything works as planned I see that editing the xml file (via Notepad++) is impossible. I receive an error message that the save failed and I should check if the file is used by another application.

No other applications apart from Notepad++ and the Powershell ISE aren't using the file.

Closing the ISE does not solve the problem (closing the ISE everytime I launch the script isn't my dream solution either).

Closing the Notepad++ doesn't solve the problem either (although the newest version remembers changes even if I don't save them, but that's not the solution either).

I suppose there is something like an XmlReader in the background that I haven't closed. My question is: how do I close it after reading the content from the xml file?

EDIT: Same script works fine if the xml is not stored on C:\ but in a lower directory. How can I make it work in the root directory C:\ ?

5
  • 1
    This doesn't happen for me on my powershell v4, there's a good chance it's being caused by something else. Commented Feb 27, 2015 at 8:40
  • 1
    Use Sysinternals' Handle to check which process is locking the XML file. Commented Feb 27, 2015 at 8:43
  • ./Handle.exe C:\PublishData.xml said "No matching handles found." Commented Feb 27, 2015 at 8:56
  • 4
    My crystal ball tells me that you launch the PowerShell ISE with elevated privileges (Run as Administrator) but not Notepad++ Commented Feb 27, 2015 at 9:37
  • @helb, you are right. That was the case. Shame on me... Commented Feb 27, 2015 at 9:50

1 Answer 1

1

Try this:

$xmlFile = 'c:\PublishData.xml'
$rawXML = Get-Content $xmlFile -Raw
$settings = [XML]$rawXML

Edit: Was just thinking, this may be another option:

$settings = [xml]([System.IO.File]::ReadAllLines($xmlFile))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.