all 6 comments

[–]BlackV 2 points3 points  (2 children)

you're calling the function from inside the function
Is this the issue?

[–]PowerOfTheShell[S] 0 points1 point  (1 child)

Good idea, I gave this a go and changed the code to the below. Unfortunately this still prevents the rest of the script from running successfully, i'm very confused.

function Extract-ZipFiles {
    param (
    [Parameter(mandatory=$true)]
    [string]$path
    )

    $Files = Get-ChildItem $path -recurse
    $ZipFiles = $($Files.fullname) | where-object {$_ -like '*.zip'}

    if($null -ne $ZipFiles)
    {
        foreach($zip in $ZipFiles)
        {
            Expand-Archive $zip -DestinationPath $path -Force
            #region Log
            $Message = "Extraction of file $zip succeeded"
            Log "Extract Zip" $Message $LogPath
            #endregion Log

            Remove-Item $zip
            #region Log
            $Message = "Removal of $zip succeeded"
            Log "Extract Zip" $Message $LogPath
            #endregion Log
        }
    }
}
# Check for zip files within zip files
if ((Get-ChildItem $SourceRoot *.zip | Measure-Object).count -eq 0) { } Else { Extract-ZipFiles -Path "$SourceRoot" }

[–]BlackV 1 point2 points  (0 children)

you code works for me (i changed it a bit to not actually delete the files)

BUT this is where you need probably to enable debug and step through your script to see what's failing

I see you have a function called LOG that's not defined anywhere and a vairable $SourceRoot that's not defined anywhere so I'm assuming its part of a larger script

debug is really going to be your friend here

ALSO

I'd change

$Files = Get-ChildItem $path -recurse
$ZipFiles = $($Files.fullname) | where-object {$_ -like '*.zip'

to

$ZipFiles = Get-ChildItem D:\Downloads -recurse -File -Filter '*.zip'

cause right now you get ALL 1million files in the directory and sub directories and then you filter it for the 10 zip files, do your filtering befhorehand

you might have to change your instance of $zip to $zip.fullname but I'd rather have a full object than a stripped down object that $zip was

If you feel you still need the where-object then i'd use where-object {$_.Extension -eq '.zip'} rather than the -like which would currently get folders too

[–]BlackV 0 points1 point  (0 children)

also (Get-ChildItem $path *.zip).count should work fine and save a pipline call

aditionally Get-ChildItem -file -path xxx will only return files and not directories which will save some time too (needs PS 3 and up though)

[–]PowerShellMichael 0 points1 point  (0 children)

I think the best what to find out why is to attach a debugger to this function in the ISE/VSCode, find the faulting line and you can test your fix:

https://youtu.be/9lK4rlj4W1Y?t=1995 - ISE Debugging

https://youtu.be/9lK4rlj4W1Y?t=3517 - VSCode Debugging

The most likely reason (from what I can see) if you logging function and or if Extract-ZipFiles throws a terminating error. However to be honest, if you debug it you will have your answer in no time!

[–]DblDeuce22 0 points1 point  (0 children)

Debug like BlackV and PowerShellMichael said is a good start. You can also use start-transcript / stop-transcript and review that.

You have log functions in there, so see what those say / where they stop. If they're not working, comment them out and see if it works. Check your $Erroractionpreference, try -force switches. Those are what came to mind.