将2个新字符串插入现有XML配置文件的Powershell脚本

将2个新字符串插入现有XML配置文件的Powershell脚本,xml,powershell,Xml,Powershell,我有一个现有的XML配置文件,但希望使用2个新字符串将该文件更新到Run1、Run2等文件中-仅针对我们整个庄园的2个VM 现有配置文件: <configuration> <configSections> <section name="Run1" type="System.Configuration.NameValueSectionHandler" /> <section name="

我有一个现有的XML配置文件,但希望使用2个新字符串将该文件更新到Run1、Run2等文件中-仅针对我们整个庄园的2个VM

现有配置文件:

<configuration>
  <configSections>
    <section name="Run1" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run3" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run4" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run5" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run6" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run7" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run8" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run9" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run10" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run11" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run12" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run13" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run14" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
   
   <Run1>
    <!-- FTP collection of AFP from QA WRE1010-->
    <add key="XsltFilePath" value="F:\OMWireTransformationService\XSLT\BBCWire2OM.xsl"/>
    <add key="InputDirectory" value="F:\WireDistribution\External\Original\OMWT1"/>
    <add key="InputFileFilter" value="*.xml"/>
    <add key="ErrorSubFolderName" value="ERROR"/>
    <add key="OutputDirectory" value="F:\WireDistribution\Outgoing"/>
    <add key="ResolveExternalDTDs" value="false"/>
    <add key="LogFilePath" value="F:\OMWireTransformationService\LOG\OMWTSLog_Run1.txt"/>
    <add key="FtpEnabled" value="sFtp"/> <!-- allowed values(first is default): "false", "true" or "Ftp", "sFtp", "sFtpCheckKnownHosts" -->
    <add key="FtpServer" value="10.57.36.80"/>
    <add key="FtpDirectory" value="/data/afp/"/> 
    <add key="FtpUser" value="omqalive"/>
    <add key="FtpPassword" value="omLIVE22!"/>
    <add key="FtpFilesRemoveEnabled" value="true"/>
    <add key="FtpTransportAsciiEnabled" value="true"/>
    <add key="FtpModeActiveEnabled" value="false"/>
    <add key="FtpPollingSeconds" value="6"/>
  </Run1>

   <Run3>
    <!-- FTP collection of internal wires -->
    <add key="XsltFilePath" value="F:\OMWireTransformationService\XSLT\BBCWire2OM.xsl"/>
    <add key="InputDirectory" value="F:\WireDistribution\External\Original\OMWT3"/>
    <add key="InputFileFilter" value="*.xml"/>
    <add key="ErrorSubFolderName" value="ERROR"/>
    <add key="OutputDirectory" value="F:\WireDistribution\Outgoing"/>
    <add key="ResolveExternalDTDs" value="false"/>
    <add key="LogFilePath" value="F:\OMWireTransformationService\LOG\OMWTSLog_Run3.txt"/>
    <add key="FtpEnabled" value="sFtp"/> <!-- allowed values(first is default): "false", "true" or "Ftp", "sFtp", "sFtpCheckKnownHosts" -->
    <add key="FtpServer" value="10.57.36.80"/>
    <add key="FtpDirectory" value="/data/omintwires/"/> 
    <add key="FtpUser" value="omqalive"/>
    <add key="FtpPassword" value="omLIVE22!"/>
    <add key="FtpFilesRemoveEnabled" value="true"/>
    <add key="FtpTransportAsciiEnabled" value="true"/>
    <add key="FtpModeActiveEnabled" value="false"/>
    <add key="FtpPollingSeconds" value="6"/>
  </Run3>

   <Run4>
    <!-- FTP collection of PA -->
    <add key="XsltFilePath" value="F:\OMWireTransformationService\XSLT\BBCWire2OM.xsl"/>
    <add key="InputDirectory" value="F:\WireDistribution\External\Original\OMWT4"/>
    <add key="InputFileFilter" value="*.xml"/>
    <add key="ErrorSubFolderName" value="ERROR"/>
    <add key="OutputDirectory" value="F:\WireDistribution\Outgoing"/>
    <add key="ResolveExternalDTDs" value="false"/>
    <add key="LogFilePath" value="F:\OMWireTransformationService\LOG\OMWTSLog_Run4.txt"/>
    <add key="FtpEnabled" value="sFtp"/> <!-- allowed values(first is default): "false", "true" or "Ftp", "sFtp", "sFtpCheckKnownHosts" -->
    <add key="FtpServer" value="10.57.40.44"/>
    <add key="FtpDirectory" value="/data/pa/"/> 
    <add key="FtpUser" value="omprodtest"/>
    <add key="FtpPassword" value="omTST88!"/>
    <add key="FtpFilesRemoveEnabled" value="true"/>
    <add key="FtpTransportAsciiEnabled" value="true"/>
    <add key="FtpModeActiveEnabled" value="false"/>
    <add key="FtpPollingSeconds" value="6"/>
  </Run4>
<add key="LogFileSizeLimit" value="40"/> // limit size for log file in Megabyte
<add key="LogFileArchiveCount" value="2"/> // how many files is desired

要添加的新字符串:

<configuration>
  <configSections>
    <section name="Run1" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run3" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run4" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run5" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run6" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run7" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run8" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run9" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run10" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run11" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run12" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run13" type="System.Configuration.NameValueSectionHandler" />
    <section name="Run14" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
   
   <Run1>
    <!-- FTP collection of AFP from QA WRE1010-->
    <add key="XsltFilePath" value="F:\OMWireTransformationService\XSLT\BBCWire2OM.xsl"/>
    <add key="InputDirectory" value="F:\WireDistribution\External\Original\OMWT1"/>
    <add key="InputFileFilter" value="*.xml"/>
    <add key="ErrorSubFolderName" value="ERROR"/>
    <add key="OutputDirectory" value="F:\WireDistribution\Outgoing"/>
    <add key="ResolveExternalDTDs" value="false"/>
    <add key="LogFilePath" value="F:\OMWireTransformationService\LOG\OMWTSLog_Run1.txt"/>
    <add key="FtpEnabled" value="sFtp"/> <!-- allowed values(first is default): "false", "true" or "Ftp", "sFtp", "sFtpCheckKnownHosts" -->
    <add key="FtpServer" value="10.57.36.80"/>
    <add key="FtpDirectory" value="/data/afp/"/> 
    <add key="FtpUser" value="omqalive"/>
    <add key="FtpPassword" value="omLIVE22!"/>
    <add key="FtpFilesRemoveEnabled" value="true"/>
    <add key="FtpTransportAsciiEnabled" value="true"/>
    <add key="FtpModeActiveEnabled" value="false"/>
    <add key="FtpPollingSeconds" value="6"/>
  </Run1>

   <Run3>
    <!-- FTP collection of internal wires -->
    <add key="XsltFilePath" value="F:\OMWireTransformationService\XSLT\BBCWire2OM.xsl"/>
    <add key="InputDirectory" value="F:\WireDistribution\External\Original\OMWT3"/>
    <add key="InputFileFilter" value="*.xml"/>
    <add key="ErrorSubFolderName" value="ERROR"/>
    <add key="OutputDirectory" value="F:\WireDistribution\Outgoing"/>
    <add key="ResolveExternalDTDs" value="false"/>
    <add key="LogFilePath" value="F:\OMWireTransformationService\LOG\OMWTSLog_Run3.txt"/>
    <add key="FtpEnabled" value="sFtp"/> <!-- allowed values(first is default): "false", "true" or "Ftp", "sFtp", "sFtpCheckKnownHosts" -->
    <add key="FtpServer" value="10.57.36.80"/>
    <add key="FtpDirectory" value="/data/omintwires/"/> 
    <add key="FtpUser" value="omqalive"/>
    <add key="FtpPassword" value="omLIVE22!"/>
    <add key="FtpFilesRemoveEnabled" value="true"/>
    <add key="FtpTransportAsciiEnabled" value="true"/>
    <add key="FtpModeActiveEnabled" value="false"/>
    <add key="FtpPollingSeconds" value="6"/>
  </Run3>

   <Run4>
    <!-- FTP collection of PA -->
    <add key="XsltFilePath" value="F:\OMWireTransformationService\XSLT\BBCWire2OM.xsl"/>
    <add key="InputDirectory" value="F:\WireDistribution\External\Original\OMWT4"/>
    <add key="InputFileFilter" value="*.xml"/>
    <add key="ErrorSubFolderName" value="ERROR"/>
    <add key="OutputDirectory" value="F:\WireDistribution\Outgoing"/>
    <add key="ResolveExternalDTDs" value="false"/>
    <add key="LogFilePath" value="F:\OMWireTransformationService\LOG\OMWTSLog_Run4.txt"/>
    <add key="FtpEnabled" value="sFtp"/> <!-- allowed values(first is default): "false", "true" or "Ftp", "sFtp", "sFtpCheckKnownHosts" -->
    <add key="FtpServer" value="10.57.40.44"/>
    <add key="FtpDirectory" value="/data/pa/"/> 
    <add key="FtpUser" value="omprodtest"/>
    <add key="FtpPassword" value="omTST88!"/>
    <add key="FtpFilesRemoveEnabled" value="true"/>
    <add key="FtpTransportAsciiEnabled" value="true"/>
    <add key="FtpModeActiveEnabled" value="false"/>
    <add key="FtpPollingSeconds" value="6"/>
  </Run4>
<add key="LogFileSizeLimit" value="40"/> // limit size for log file in Megabyte
<add key="LogFileArchiveCount" value="2"/> // how many files is desired
//以MB为单位限制日志文件的大小
//需要多少个文件

但我希望这些新的字符串正好在两个字符串之间。到目前为止,您尝试了什么?请显示您的代码。$pathToConfig=“F:\OMWireTransformationService\OMWireTransformationService.exe.config”#将您的路径放在这里#强制将配置转换为XML$XML=[XML](获取内容$pathToConfig)#在$foundNode=$XML.configuration.Run1之后找到要插入的节点#手动构建新节点并强制将其转换为XML对象$newNode=[XML]@”@#在configsections节点$newNode=$xml.ImportNode($newNode.appSettings,$true)$xml.configuration.InsertAfter($newNode,$foundNode)| out null#save file$xml.save($pathToConfig.CHANGED.xml))之后添加新节点,但我希望它在配置文件签出xPath中的所有部分(Run1、Run2、Run3等)中运行,使用selectNodes,您可以循环浏览所有点击