【发布时间】:2018-09-30 09:41:42
【问题描述】:
我是解析 XML 的 Powershell 脚本的新手,我刚刚遇到了一种情况,我需要确保一行 XML 在另一行之前严格执行。
为了到达那里,我首先搜索了我将如何读取 XML 文件,这就是我得到的:
[XML]$Web = Get-Content *path_to_XML_file*
使用点符号,我前往包含需要验证的行的节点(我仍然不知道如何找到行号以确定它是在另一个之前还是之后)。
我需要迭代的节点位于:
$Web.Configuration.location.'system.WebServer'.modules
当我输入最后一行时,这是一个控制台输出:
add
---
{HttpCacheModule, DynamicCompressionModule, IsapiFilterModule, ProtocolSupportModule...}
此节点包含名为“add”但类型为 System.Array 的其他节点的多个实例。
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Object[] System.Array
此外,这是 XML 中模块节点的副本:
<modules>
<add name="HttpCacheModule" lockItem="true" />
<add name="DynamicCompressionModule" lockItem="true" /> <--HERE
<add name="IsapiFilterModule" lockItem="true" />
<add name="ProtocolSupportModule" lockItem="true" />
<add name="StaticFileModule" lockItem="true" />
<add name="AnonymousAuthenticationModule" lockItem="true" />
<add name="DefaultDocumentModule" lockItem="true" />
<add name="CustomErrorModule" lockItem="true" />
<add name="HttpRedirectionModule" lockItem="true" />
<add name="RequestFilteringModule" lockItem="true" />
<add name="IsapiModule" lockItem="true" />
<add name="ConfigurationValidationModule" lockItem="true" />
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule" preCondition="managedHandler" />
<add name="Session" type="System.Web.SessionState.SessionStateModule" preCondition="managedHandler" />
<add name="WindowsAuthenticationModule" lockItem="true" />
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" preCondition="managedHandler" />
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" preCondition="managedHandler" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" preCondition="managedHandler" />
<add name="RoleManager" type="System.Web.Security.RoleManagerModule" preCondition="managedHandler" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" preCondition="managedHandler" />
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" preCondition="managedHandler" />
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" preCondition="managedHandler" />
<add name="Profile" type="System.Web.Profile.ProfileModule" preCondition="managedHandler" />
<add name="UrlMappingsModule" type="System.Web.UrlMappingsModule" preCondition="managedHandler" />
<add name="BasicAuthenticationModule" lockItem="true" />
<add name="HttpLoggingModule" lockItem="true" />
<add name="StaticCompressionModule" lockItem="true" />
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" preCondition="managedHandler,runtimeVersionv2.0" />
<add name="ServiceModel-4.0" type="System.ServiceModel.Activation.ServiceHttpModule, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" preCondition="managedHandler,runtimeVersionv4.0" />
<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="managedHandler,runtimeVersionv4.0" />
<add name="ScriptModule-4.0" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" preCondition="managedHandler,runtimeVersionv4.0" />
<add name="ClientLoggingHandler" />
<add name="AdvancedLoggingModule" />
<add name="AppWarmupModule" />
<add name="RewriteModule" /> <---- HERE
<add name="FailedRequestsTracingModule" lockItem="true" />
<add name="F5XFFHttpModule" />
</modules>
我需要确保<add name="DynamicCompressionModule" lockItem="true" /> 行绝对在<add name="RewriteModule" /> 之前执行,并且我不知道如何遍历此 System.Array 以到达那里。
有人可以帮我吗?
【问题讨论】:
-
我会尝试使用 XPath 来编写它。然后我可以使用
Select-Xml。
标签: xml powershell powershell-2.0