6
\$\begingroup\$

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"
 }
}
asked Jun 15, 2014 at 16:50
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

answered Jun 16, 2014 at 0:49
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.