Private/Test-NetFrameworkProjectFile.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function Test-NetFrameworkProjectFile
{
    Param(
        [Parameter(ValueFromPipeline)]
        $InputObject
    )

    [bool]$accept = $false;
    $path = ConvertTo-Path $InputObject;
    
    if ($path -and ($path.EndsWith("proj")))
    {
        [xml]$doc = $null;
        try { $doc = Get-Content $path; } catch { return $false; }
        $ns = [System.Xml.XmlNamespaceManager]::new($doc.NameTable);
        $ns.AddNamespace("x", "http://schemas.microsoft.com/developer/msbuild/2003");

        try 
        { 
            $valid = ($doc.SelectSingleNode("//x:Project", $ns) -ne $null); 
            if ($valid) 
            {
                return [PSCustomObject]@{
                    "Xmlns"=$ns;
                    "Document"=$doc;
                    "Path"=$path;
                };
            }
        }
        catch { return $false; } 
        
    }
    return $false;
}