lontivero
lontivero

Reputation: 5275

XML element not found in PS script

I am trying to use this powershell script [https://gist.github.com/557222] in order to analyse my VS solution references because I have a conflict but I don't know where.

The problem is it doesn't work because this error:

ForEach-Object : Property 'HintPath' cannot be found on this object. Make sure that it exists.
At C:\bsi\Projects\Bsi.ManagementConsole\Branches\Tango58\Get-VSSolutionReferences.ps1:37 char:31
+                 ForEach-Object <<<<  {
    + CategoryInfo          : InvalidOperation: (.:OperatorToken) [ForEach-Object], RuntimeException
    + FullyQualifiedErrorId : PropertyNotFoundStrict,Microsoft.PowerShell.Commands.ForEachObjectCommand

It seems in some cases the XML doesn't have any HintPath element. If I remove the offending lines the script runs okay. However, it doesn't provides the most relevant information that I need: the HintPath.

So, what I need is have that info when it is possible. I mean, if the XML element has the HintPath element I need it, when it doesn't show me nothing.

I am asking this because I don´t know Powershell and it doesn't matter how hard I try, I couldn't do it work as I want.

Could you help me, please?

Upvotes: 1

Views: 791

Answers (1)

Keith Hill
Keith Hill

Reputation: 201652

Comment out the line Set-StrictMode -Version Latest since that causes an error when you try to access property HintPath on an object and the property doesn't exist. Or you can modify the check on $_.HintPath to this:

if (($_.psobject.Properties.Match('HintPath')) -ne $null -and $_.HintPath) {
    $RefPath = $ProjectRoot | Join-Path -ChildPath $_.HintPath
}

But from my simple testing, you would need to do this in other parts of the script as well e.g. SpecificVersion could not be found.

Upvotes: 2

Related Questions