1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
| function ForEach-Parallel {
param(
[Parameter(Mandatory=$true,position=0)]
[System.Management.Automation.ScriptBlock] $ScriptBlock,
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[PSObject]$InputObject,
[Parameter(Mandatory=$false)]
[int]$MaxThreads=5
)
BEGIN {
$iss = [system.management.automation.runspaces.initialsessionstate]::CreateDefault()
$pool = [Runspacefactory]::CreateRunspacePool(1, $maxthreads, $iss, $host)
$pool.open()
$threads = @()
$ScriptBlock = $ExecutionContext.InvokeCommand.NewScriptBlock("param(`$_)`r`n" + $Scriptblock.ToString())
$countjob = 0
$Results = @()
#"Initialisation" | Out-Host
}
PROCESS {
$powershell = [powershell]::Create().AddScript($scriptblock).AddArgument($InputObject)
$powershell.runspacepool = $pool
$threads += @{
instance = $powershell
handle = $null
#handle = $powershell.begininvoke()
}
$countjob++
}
END {
foreach ($thread in $threads)
{
$thread.handle = $thread.instance.begininvoke()
}
#"Début traitement" | Out-Host
$finishthread= 0
$notdone = $true
while ($true) {
$notdone = $false
for ($i=0; $i -lt $threads.count; $i++) {
$thread = $threads[$i]
if ($thread) {
if ($thread.handle.iscompleted) {
$Result = $thread.instance.EndInvoke($thread.handle)
$Results += $Result
# $Result | Export-Csv -Path c:\Mutextest.csv -NoTypeInformation -Force -Append
$thread.instance.dispose()
$threads[$i] = $null
$finishthread++
}
else {
$notdone = $true
}
}
}
#"$finishthread / $countjob" | Out-Host
if (!$notdone)
{break}
Start-Sleep -Milliseconds 50
continue
}
Return $Results
}
}
#Recherche du fichier spécifique
$file2 = "C:\Users\*.txt"
#Initialisation des différentes variables permettant de savoir le nombre total de fichiers et la taille total se trouvant dans $file2
$NumberOfFilesTotal = 0
$tailleTotal = 0
$Paths = New-Object System.Collections.ArrayList($null)
foreach($file in Get-ChildItem -path $file2 -force -ErrorAction SilentlyContinue | Select-Object -ExpandProperty FullName)
{
foreach($line in [System.IO.File]::ReadLines($file, [System.Text.Encoding]::Default))
{
$paths.Add($Line) > $null
}
}
$paths = $paths | Select-Object -Unique
$result = New-Object System.Collections.ArrayList($null)
$result = $paths | ForEach-Parallel -MaxThreads 50 {
if([System.IO.File]::Exists($_))
{
return [System.IO.FileInfo]$_
}
}
$ret = $result | Measure-Object -Sum Length
$NumberOfFilesTotal = $ret.Count
$tailleTotal = $ret.Sum
#Conversion de bytes en MégaBytes
$tailletotal = $tailleTotal / 1048576
#Ecriture du résultat à l'écran
Write-Host "Il y a" $NumberOfFilesTotal "fichiers" -ForegroundColor Yellow
Write-Host "Les fichiers font" $tailleTotal "MB" -ForegroundColor Green
exit |