如果存在另一个xml节点,请检查xml节点是否存在

时间:2012-01-19 15:11:34

标签: powershell

我有一个目录作为命令行参数传递给power shell脚本。我需要递归搜索扩展名为csprj的文件,并检查Project / ItemGroup / Reference节点下是否存在HintPath节点。如果存在HintPath节点,请检查另一个名为Private的节点是否存在,其值为False,并以错误代码0退出。在ItemGroup下可能有多个参考节点

是否可以创建一个电源shell脚本来执行此操作?下面给出了一个示例xml文件

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"
         ToolsVersion="3.5">
  <ItemGroup>
    <Reference Include="IronPython, Version= ...">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\shared\IronPython-1.1\IronPython.dll</HintPath>
    </Reference>
    <Reference Include="log4net, Version= ...">
      <SpecificVersion>False</SpecificVersion>
      <HintPath>..\binaries\log4net.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>
</Project>

1 个答案:

答案 0 :(得分:1)

gci . -filter *.csproj -recurse | foreach-object {
  $filename = $_.fullname 
  [xml]$sample = gc $_.fullname
  $sample.Project.ItemGroup.Reference | 
  select @{N="filename";E={$filename}}, hintpath, private 
}

应该给你一个这样的概述:

filename                                 HintPath                                private
--------                                 --------                                -------
C:\users\mytest\desktop\sample1.csproj   ..\shared\IronPython-1.1\IronPython.dll        
C:\users\mytest\desktop\sample1.csproj   ..\binaries\log4net.dll                 False  
C:\users\mytest\desktop\sample2.csproj   ..\shared\IronPython-1.1\IronPython.dll        
C:\users\mytest\desktop\sample2.csproj   ..\binaries\log4net.dll                 False