3

I'm trying to run a PowerShell file delete operation that deletes files based on specific parameters. So, the code goes like this:

$sb = {
    Get-ChildItem -Path $SubFolder | Remove-ChildItem
    if ($Error[0] -eq $null) {
        $results =  New-Object PSObject -Property @{
            $Foldername = $Subfolder.Name
            $TotalFiles = $SubFolder.(Count)
        }
    } else { 
        $errorresult = "Error deleting files from $($Subfolder.Name)"
    }

    #Error Mutex
    $ErrorLogmutex = New-Object System.Threading.Mutex($false, "ErrorLogFileHandler")
    $ErrorLogmutex.WaitOne()
    Out-File -Append -InputObject $errorresult -FilePath $Errorlog
    $ErrorLogmutex.ReleaseMutex()

    #Success Mutex
    $Successmutex = New-Object System.Threading.Mutex($false, "SuccessFileHandler")
    $SuccessLogmutex.WaitOne()
    Out-File -Append -InputObject $results -FilePath $successlog
    $Successmutex.ReleaseMutex()
}

#Calling scriptblock in multi-thread count of 5
{
    foreach ($Subfolder in $Folder) {
        while ((Get-Job -State Running).Count -ge 5) {
            Start-Sleep -Seconds 5
        }
        Start-Job -ScriptBlock $sb -ArgumentList $arguments | Out-Null
}

The script runs, able to see the results if I explicitly call out Receive-Job -Name JobID, but the output does not produce any log files as I would've thought it would.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
mutukrp
  • 97
  • 1
  • 9

0 Answers0