Selecting an XML Element by #text value

This is actually just a wrapper for the [text()=’value’] XPATH attribute.  As I said in a previous post, this blog is my own knowledge base.  When I need this, and I’m sure I will, I’ll remember writing this entry, search my blog, and there it is!

This will be used in a subsequent post about getting the hashed credentials string out of an RDG file.

 function Select-ElementByText
{
    param (
        [Parameter(ValueFromPipeline=$true)][String[]]$Name,
        [Parameter()]$Path
    );

    begin
    {
        if (!$Path)
        {
            Write-Warning "$($MyInvocation.MyCommand.Name) -Path not specified.  Stopping.";
            break __outOfScript;
        } # if (!$Path)

        if (!(Test-Path -Path $Path))
        {
            Write-Warning "$($MyInvocation.MyCommand.Name) -Path '$Path' not found. Stopping.";
            break __outOfScript;
        } # if (!(Test-Path -Path $Path))

        if (!($xml = (Get-Content $Path) -as [xml]))
        {
            Write-Warning "$($MyInvocation.MyCommand.Name) -Path '$Path' cannot be parsed as XML.  Stopping.";
            break __outOfScript;
        } # if (!($xml = (Get-Content $Path) -as [xml]))

    } # begin

    process
    {
        foreach ($myName in $Name)
        {
            $xml.SelectNodes("//*[text()='$myName']");
        } # foreach ($myName in $Name)

    }
    # process

} # function Select-ElementByText