Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: using wildcards with add-type

$
0
0

You can enumerate the GAC and do your own pattern matching to find the actual assembly name. It looks like you either need to use P/Invoke to call methods of the native GAC API for that, or have a copy of gacutil.exe available for your PowerShell script to call. Here’s what I tried on my machine as a test. Sorry if it’s a little hard to read; I have a love affair with regular expressions. :P

The important thing is that the pattern matches the entire string returned by gacutil (which is why I have the “.*” right before the closing parenthesis in the regex pattern, and the pattern begins and ends with ^ $). In this case, I chose to extract the text without any leading or trailing whitespace, as well (which is what the parentheses are for, and why I’m using $matches[1] instead of $matches[0]).

$assemblyRegex = '^\s*(' + 
                 'WebMatrix.Data\s*,' +
                 '\s*Version\s*=\s*2\..*\..*\..*\s*,' + 
                 '\s*Culture\s*=\s*neutral\s*,' + 
                 '\s*PublicKeyToken\s*=\s*31bf3856ad364e35' +
                 '.*)\s*$'

Set-Location -Path 'C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools'

$assemblyName = "<Not Found>"

$assemblies = .\gacutil.exe /L

foreach ($assembly in $assemblies)
{
    if ($assembly -match $assemblyRegex)
    {
        $assemblyName = $matches[1]
        break
    }
}

Write-Host "Detected assembly name: '$assemblyName'"

Viewing all articles
Browse latest Browse all 13067

Trending Articles