Maanu
Maanu

Reputation: 5203

Check presence of an xml node if another xml node is present

I have a directory passed as a command line parameter to the power shell script . I need to recursively search for files with csprj extension and check whether HintPath node is present under Project /ItemGroup/Reference node. If HintPath node is present, check the presence of another node named Private with value False and exit with error code 0. There could be multiple Reference nodes under ItemGroup

Is it possible to create a power shell script to do this? A sample xml file is given below

<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>

Upvotes: 0

Views: 582

Answers (1)

jon Z
jon Z

Reputation: 16646

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

Should give you an overview like this:

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 

Upvotes: 1

Related Questions