Powershell : Monitor Folder and run Payload on "new folders"

I was working on the previous article about zipping up files with 7zip with a password based on the name of the folder, and then though to myself, would it not be nice if I could have a script to "monitor" a folder every 180 seconds.

Then when changes are detected run a payload - which in this case was the 7zip script and then delete the folder when done, that sounds like I need some Skeletor Powershell script skills, so by the power of greyskull lets get cracking.

$folderPath = "<folder to monitor>"
$secondaryScriptPath = "<script to run in ps1>"

# Function to execute the secondary script
function ExecuteSecondaryScript {

    param (
        [string]$scriptPath
    )

    Write-Host "Executing secondary script: $scriptPath"
    $output = Start-Process powershell.exe -ArgumentList "-File $scriptPath" -NoNewWindow -Wait -PassThru
    if ($output.ExitCode -eq 0) {
        return $true
    } else {
        return $false
    }
}

# Function to delete subfolders
function DeleteSubfolders {
    param (
        [string[]]$folders
    )
    foreach ($folder in $folders) {
        Write-Host "Deleting folder: $folder"
        Remove-Item -Path $folder -Recurse -Force
    }
}

# Function to monitor for new subfolders
function MonitorFolder {
    param (
        [string]$path,
        [string]$scriptPath
    )
    $subfolders = Get-ChildItem -Path $path -Directory
    if ($subfolders -eq $null) {

# Folder is empty, wait and continue monitoring

Write-Host "Folder is empty. Waiting for new subfolders..."
Start-Sleep -Seconds 120
MonitorFolder -path $path -scriptPath $scriptPath
return
    }

$subfolderCount = $subfolders.Count

# Wait for 2 minuutes
Start-Sleep -Seconds 120

# Check if new subfolders have been added

$newSubfolders = Get-ChildItem -Path $path -Directory
    if ($newSubfolders -eq $null) {

# No new subfolders found, continue monitoring

MonitorFolder -path $path -scriptPath $scriptPath
        return
    }
$newSubfolderCount = $newSubfolders.Count
if ($newSubfolderCount -gt $subfolderCount) {

$addedSubfolders = Compare-Object -ReferenceObject $subfolders -DifferenceObject $newSubfolders -Property Name -PassThru

        foreach ($subfolder in $addedSubfolders) {

            Write-Host "New subfolder detected: $($subfolder.FullName)"
            if (ExecuteSecondaryScript -scriptPath $scriptPath) {

# Delete subfolders after the secondary script has run successfully

DeleteSubfolders -folders $addedSubfolders.FullName
            } else {
                Write-Host "Secondary script execution failed or did not indicate success."
            }
        }
    }

# Restart monitoring

MonitorFolder -path $path -scriptPath $scriptPath
}

# Start monitoring
MonitorFolder -path $folderPath -scriptPath $secondaryScriptPath

IMPORTANT : In your payload script after the code is run you need to add this to it, else the main code will not know when it has terminated correctly!

# After completing the tasks successfully, exit with code 0 to indicate success
exit 0

Previous Post Next Post

Ω†Ω…ΩˆΨ°Ψ¬ Ψ§Ω„Ψ§ΨͺΨ΅Ψ§Ω„