I'm trying to get the dll and exe file names in the windows system directory (C:\Windows\System32).
There are too many files in that path, so I'm trying to use multithreading.
However, when comparing(at "IF" statement) extensions in ScriptBlock added by Addscript, only "False" result occurs, so the file name cannot be added into the list.
if ($extensionList -contains $tmpExtension.ToString())
Maybe the conditional statement is wrong or the ScriptBlock is wrong? The conditional statement works normally when multithreading is not used.
Entire code(Powershell 5.1) :
$extensionList = @(".DLL",".EXE")
$systemDir = "C:\Windows\System32\"
$threadNumber = 4 #threads
$tempList = @()
$ScriptBlock = {
Param ($tmpFilePath)
$tmpExtension = [IO.Path]::GetExtension($tmpFilePath)
if ($extensionList -contains $tmpExtension.ToString()) {
Return $tmpFilePath
}
Return $null
}
$RunspacePool = [RunspaceFactory]::CreateRunspacePool(1, $threadNumber)
$RunspacePool.Open()
$Jobs = @()
$tmpfileNameList = (Get-ChildItem -Path $systemDir -File) | select-object Name
foreach($tmpfileName in $tmpfileNameList) {
$tmpFilePath = $systemDir + $tmpfileName.Name
$Job = [powershell]::Create().AddScript($ScriptBlock).AddArgument($tmpFilePath)
$Job.RunspacePool = $RunspacePool
$Jobs += New-Object PSObject -Property @{
Pipe = $Job
Result = $Job.BeginInvoke()
}
}
while ($Jobs.IsCompleted -contains $false) {Start-Sleep -Milliseconds 100}
$Results = @()
ForEach ($Job in $Jobs){
if ($Job.Pipe.EndInvoke($Job.Result) -ne $null) {
$tempList += $Job.Pipe.EndInvoke($Job.Result)
}
}
foreach($tmp in $tempList) {
Write-Host $tmp
}