I often write different applications in C++ using different libraries, and sometimes it takes a lot of time to find where are the *.dll
files to distribute. I use Dependency Walker but it still takes a lot of time to look in the GUI. I wrote a PowerShell script that will find all used libraries and do something with them. Please review my code.
Param
(
[Parameter(Mandatory=$true, Position=0)]
[ValidateScript({Test-Path $_ -PathType Leaf})]
[String]$Path,
[Parameter()]
[ValidateScript({Test-Path $_ -PathType Container})]
[String[]]$Exclude
)
function likeAnyOf($obj,$array)
{
foreach($item in $array)
{
if($obj -like $item)
{
return $item
}
}
return $null
}
Set-StrictMode -Version Latest
$systemPath = [System.Environment]::SystemDirectory
$tempName = [System.IO.Path]::GetTempFileName() # generate a temporary filename
Start-Process depends.exe -ArgumentList '/c','/f:1',"/oc:$tempName",$Path -Wait -NoNewWindow # wait until it writes all the data
$modules = Import-Csv $tempName -Encoding Default | Select-Object -Property Module -Unique # read the output of depends.exe and select all unique DLL paths
Remove-Item -Path $tempName -Force # delete the temporary file
$dllsToCopy = @{}
foreach($module in $modules)
{
$modulePath = $module.Module
$moduleParent = Split-Path $modulePath -Parent
$moduleName = Split-Path $modulePath -Leaf
if( $moduleParent -like $systemPath )
{
Write-Verbose "Skipped $moduleName as a system module ($moduleParent)"
continue
}
$excluded = likeAnyOf $moduleParent (Resolve-Path $Exclude)
if($excluded -ne $null)
{
Write-Host "Skipped $moduleName as explicitly excluded ($excluded)"
continue
}
$exists = Test-Path $modulePath -PathType Leaf
if($exists)
{
$dllsToCopy.Add($moduleName, $modulePath)
Write-Host "Added $moduleName found at $moduleParent"
}
else
{
Write-Error "Couldn't find $moduleName"
}
}