Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
281 views
in Technique[技术] by (71.8m points)

c# - Edit web.config using get-content and replace in powershell

I'm new to Powershell commands and I use get-content and then .replace to edit part of web.config on server, I wanted to know if this method of editing web.config is safe or not?

thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In a word, no.

web.config is an XML file, and should be edited as an XML document. The file contents are text (instead of binary), but the catch is that the XML element structure must be maintained. Editing the file as text is prone to mistakes, such as mixing up open/close element order, missing elements and encoding characters that are reserved.

If one makes a single mistake in editing XML as it would be an ordinary text file, any application that expects it to be a valid XML document will throw an error as the file is not valid.

To be honest, it is possible to update an XML config as it would be a text file. Why take the risk, though? Since .Net, and thus Powershell, is capable of parsing and processing XML, load the config file as an XML document, update the contents and save it. By processing it as an XML document, .Net libraries will take care about how to process the file so that the result is always a valid XML file.

There are existing questions at, SO, try searching for "powershell web.config $setting-you-want-to-change" for similar cases.

Also, the config can be updated via web administration cmdlets, for example, adding SSL settings.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...