HiveBrain v1.2.0
Get Started
← Back to all entries
patternshellMinor

PowerShell script to automate the search of DLL files

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
scriptthedllsearchpowershellautomatefiles

Problem

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

Solution

You can replace all this:

function likeAnyOf($obj,$array)
{
    foreach($item in $array) 
    { 
        if($obj -like $item) 
        {
            return $item
        } 
    }
    return $null
}

# ...

$excluded = likeAnyOf $moduleParent (Resolve-Path $Exclude)


With this:

$excluded = Resolve-Path $Exclude | ? { $moduleParent -like $_ }


The question mark (?) is an alias for Where-Object, so read up on that for an explanation.

Instead of this:

if ($excluded -ne $null)
{
    # ...
}


you can do this:

if ($excluded)
{
    # ...
}


It's just a little bit easier to read.

Code Snippets

function likeAnyOf($obj,$array)
{
    foreach($item in $array) 
    { 
        if($obj -like $item) 
        {
            return $item
        } 
    }
    return $null
}

# ...

$excluded = likeAnyOf $moduleParent (Resolve-Path $Exclude)
$excluded = Resolve-Path $Exclude | ? { $moduleParent -like $_ }
if ($excluded -ne $null)
{
    # ...
}
if ($excluded)
{
    # ...
}

Context

StackExchange Code Review Q#54315, answer score: 3

Revisions (0)

No revisions yet.