Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

XML question

$
0
0

Hi all, I have a script to search for a specific value in XML file and add them to custom object. This will run through lots of xml files and some files may not have that value. I want help with a good way to ignore if the value(s) not found or exist. here is the script.

cls$serverarch_obj = @()$sourceFiles = 'C:\temp'$xmlFiles = Get-ChildItem -Path $SourceFiles -Recurse -Include *.xml -ErrorAction stopforeach ($xmlFilein$xmlFiles)
{[xml]$xmlcontent = Get-Content$xmlFile.FullName$Gcontent = $xmlcontent.report.general.entity | select Name, Value$NV = $xmlcontent.report.check.entity | select name, Value$Result = [Ordered]@{}$objresult = New-Object psobjectif ($($Gcontent.Value[0]) -ne$null)
    {Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'Start Time' -Value $($Gcontent.Value[0]) 
    }if ($($Gcontent.Value[7]) -ne$null)
    {Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'End Time' -Value $($Gcontent.Value[7]) 
    }if ($($Gcontent.name[6]) -ne$null)
    {Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'Result' -Value $($Gcontent.name[6])
    }if ($NV.value[3].Substring(101,2) -ne$null)
    {Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'Country' -Value "$($NV.value[3].Substring(101,2))"
    }if ($($Gcontent.value[5].Substring(11)) -ne$null)
    {Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'User' -Value "$($Gcontent.value[5].Substring(11))"
    }if ($($NV[6].value.Substring(95)) -ne$null)
    {Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'Environment' -Value "$($NV[6].value.Substring(95))"
    }if ($($NV[1].value) -ne$null)
    {Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'Tool' -Value  "$($NV[1].value)"
    }Add-Member -InputObject $objresult -MemberType 'NoteProperty' -Name 'File' -Value  $xmlFile.FullName$serverarch_obj += $objresult
}$serverarch_obj

The error i get is:

Exception calling "Substring" with "1" argument(s):"startIndex cannot be larger than length of string.Parameter name: startIndex"
At C:\temp\script.ps1:39 char:11
+     if ($($NV[6].value.Substring(95)) -ne$null)
+           ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Viewing all articles
Browse latest Browse all 6937

Trending Articles