Deleting multiple files from multiple computers not working as expected

Home Forums Scripting PowerShell Deleting multiple files from multiple computers not working as expected

This topic contains 10 replies, has 3 voices, and was last updated by Avatar crocodile 5 months, 3 weeks ago.

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • Avatar
    crocodile
    Participant
    #617239

    I have this script that I have compiled from different scripts to get exactly what I need.

    The filelist looks something like this:
    D$\Test1\Test1\*
    D$\Test2\Test1\*

    Now I want to delete the file in all of the subfolders of Test1 and Test2, but not actually delete any subfolders. At this stage I am specifying each subfolder in the filelist, but that could be problematic in the future, as I would have to constantly monitor the folders to see if there are any subfolders added and then add them to the filelist. Ideally I need to only specify the folder, the script needs to go through all subfolders and delete all files, but not subfolders. The file count is also not working as expected, either reporting 1 or 0, which I guess is counting the folder and not the files. Any help to improve this would be highly appreciated.

    function Write-Log($string)

    {

    $outStr = “” + $Timestamp +” “+$string

     

    Write-Output $outStr

     

    }

     

    $Timestamp = Get-Date -Format “yyyy-MM-ddTHH-mm-ss”

    $filelist = Get-Content C:\support\scripts\filelist.txt

    $computerlist = Get-Content C:\support\scripts\computerlist.txt

    $Log = “c:\support\scripts\logs\Delete_Old_Files_$(Get-Date -Format ‘yyyyMMddhhmmss’).log”

     

    Start-Transcript -path $Log -append -Force -NoClobber

    Write-Log “———— Start of Log  ————”

     

    foreach ($file in $filelist) {

    foreach ($computer in $computerlist){

    Write-Log “Analysing $computer”

     

    $newfilepath = Join-Path “\\$computer\” “$file”

    if (test-path $newfilepath) {

    Write-Log “$newfilepath folder exists”

    try

    {

    Remove-Item $newfilepath -Recurse -force -ErrorAction Stop | Where-Object { -not ($_.psiscontainer) }

    }

    catch

    {

    $ErrorMessage = $_.Exception.Message

    $FailedItem = $_.Exception.ItemName

    Send-MailMessage -From [email protected]@aaa.com -To [email protected] -Subject “Old Files Delete Failed!” -SmtpServer bla.com -Body “The error message is: ‘$ErrorMessage'”

    Break

     

    }

    Write-Log “$newfilepath files deleted”

    } else {

    Write-Log “Path $newfilepath no files to delete”

    }

    # output statistics

    Write-Output “**********************”

    #Write-Output “Number of old files deleted: $($_.Count)”

    Write-Log “————- End of Log ————-”

    }

    }

     

     

    Stop-Transcript

    • This topic was modified 6 months ago by Avatar crocodile.
    JeremyW
    JeremyW
    Moderator
    #617273

    Filtering after you remove the item won’t really work.

    Can you try something like this:

    Avatar
    crocodile
    Participant
    #617284

    Thank you

    I will try that

    Avatar
    crocodile
    Participant
    #617285

    Cannot get it working
    How do I incorporate the multiple computers into that?

    JeremyW
    JeremyW
    Moderator
    #617288

    I haven’t tested this but I took your script and modified it. Let me know if you have any questions:

    • This reply was modified 6 months ago by JeremyW JeremyW. Reason: Typo in script
    • This reply was modified 6 months ago by JeremyW JeremyW. Reason: Fix double quotes
    Avatar
    crocodile
    Participant
    #617293

    Thank you, will test on Monday
    Much appreciated

    Avatar
    wullieb1
    Moderator
    #617532

    Just an FYI there is apparently a bug in the -recurse option for Remove-Item

    The fix Jeremy has in his code should work fine.

    Avatar
    crocodile
    Participant
    #617577

    It is working good, thank you for that.
    How do I specify all subfolders?
    I have a few subfolders under Test1 and Test2 and it would be a lot easier than having to specify Test1\AAA, TEST1\BBB, etc
    Also, can i use this script to copy files as well?
    Do I just change the Remove-item line?

    Thanks for all the help

    Avatar
    crocodile
    Participant
    #617578

    Oh, yes. I get this error as well:
    Remove-Variable : Cannot bind argument to parameter ‘Name’ because it is null.

    Avatar
    crocodile
    Participant
    #617579

    Disregard my comment about specifying subfolders
    After rebooting my server, it is all working as expected

    Avatar
    crocodile
    Participant
    #617580

    Still getting the Remove-Variable error
    I have disabled that line for now and it is working well.

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.