Friday, November 19, 2010

PowerShell script to get a list of all assemblies required by a .NET executable

param ([string][parameter(mandatory=$true)]$path)

function GetReferencedAssemblies([reflection.assembly][parameter(mandatory=$true)]$top)
{
    $script:found += $top.Location

    $ref = $top.getreferencedassemblies() 
    $ref | % {
        $name = $_.name
        $loaded = $null
        try
        {
            $loaded = [reflection.assembly]::load($_) 
        }
        catch
        {
            $loaded = [reflection.assembly]::loadfrom((resolve-path ($name + '.dll')))
        }

        if($script:found -notcontains $loaded.Location -and -not $loaded.GlobalAssemblyCache)
        {
            GetReferencedAssemblies $loaded
        }
    }
}

$path = resolve-path $path -ErrorAction stop
pushd (split-path $path)
try
{
    $found = @()
    $top = [reflection.assembly]::loadfrom($path)
    GetReferencedAssemblies $top
    $found
}
finally
{
    popd
}

0 comments:

Post a Comment