Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeleteOlderThanDays #31

Open
AlfredHSwe opened this issue Jan 11, 2021 · 22 comments
Open

DeleteOlderThanDays #31

AlfredHSwe opened this issue Jan 11, 2021 · 22 comments

Comments

@AlfredHSwe
Copy link

Hi,
I´ve just tried your script and the shrink function works perfect.
But I cant seem to delete VHDX files that are really old.
When I run
.\Invoke-FslShrinkDisk.ps1 -Path "path to profile" -DeleteOlderThanDays 30
Nothing happens. The profile was last used in 2020-06-11.
What am I missing?

Thanks in advance

@StevenNoel
Copy link

experiencing the same thing. We run this script weekly and set the 'deleteolderthandays' to 90....however each week when the script runs, it changes the VHDX modified date, thus never reaching the 'deleteolderthandays' value. Was thinking of looking at last modified date on ntuser.dat or .ost file, but since it's not mapping a drive, I'm not sure what the best way would be. Anyone have any thoughts?

@MrFly72
Copy link

MrFly72 commented Sep 1, 2021

There would be an easy solution for this:
$FileWriteTimeUTCBeforeShrink=(Get-Item -Path $VHDXFileName).LastWriteTimeUTC
Then after the shrink:
$FileInfosAfterShrink=Get-Item -Path $VHDXFileName
$FileInfosAfterShrink.LastWriteTimeUTC=$FileWriteTimeUTCBeforeShrink
Did not look at the script thoroughly, but I believe that the script is reading LastWriteTime/UTC.
I am right before my yearly holiday, so no time to look more than this.

Edit:
Just searched the code:
$mostRecent = $Disk.LastAccessTime, $Disk.LastWriteTime | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum
So you should reset both values to the values before the shrink, LastWriteTime + LastAccessTime

@RyanSwanepoel
Copy link

We are experiencing the same issue, One thought I had would be to do the check on the .meta file? as that should only update when the user has logged in and attached their profile?

@StevenNoel
Copy link

yeah, or ntuser.dat or something like that.

@RyanSwanepoel
Copy link

RyanSwanepoel commented Feb 11, 2022

For ntuser.dat you would still need to mount the .vhdx to check the time stamp though no? Wouldn't mounting it still change the modified stamp?

@StevenNoel
Copy link

Looks like the meta file is only for the cloud cache feature. If it's outside the VHDX file then yeah, that would be ideal. For customers that aren't using cloud cache, they might not have a choice other than to mount.

@RyanSwanepoel
Copy link

Ah good catch, we are using Cloud Cache at the moment and it sits outside the .VHDX, only thing is I don't understand the Powershell enough to add in the check against the meta file.

@StevenNoel
Copy link

MrFly had a good suggestion above, that would work.

Or you could do a get-childitem on the meta file, to grab the lastAccessTime and LastWriteTime values. Then do a condition in the code below if the meta file exists, use that files LastAccessTime and LasteWriteTime instead of the VHDX file.

    #If it's older than x days delete disk
    If ( $DeleteOlderThanDays ) {
        #Last Access time isn't always reliable if diff disks are used so lets be safe and use the most recent of access and write
        $mostRecent = $Disk.LastAccessTime, $Disk.LastWriteTime | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum
        if ($mostRecent -lt (Get-Date).AddDays(-$DeleteOlderThanDays) ) {
            try {
                Remove-Item $Disk.FullName -ErrorAction Stop -Force
                Write-VhdOutput -DiskState "Deleted" -FinalSize 0 -EndTime (Get-Date)
            }
            catch {
                Write-VhdOutput -DiskState 'Disk Deletion Failed' -EndTime (Get-Date)
            }
            return
        }
    }

@RyanSwanepoel
Copy link

Yeah I was thinking about using it but I don't know where to add the Get-Childitem to find the meta file. Will do some more digging and see where I can put it.

@JimMoyle
Copy link
Contributor

JimMoyle commented Feb 11, 2022 via email

@RyanSwanepoel
Copy link

Hi Jim thanks for the response!

Am I correct in thinking I would just need to add the changes in commit (5dc8aa7) to both Dismount-FSLDisk.ps1 and Optimize-OneDisk.ps1? which will then set the LastWriteTimeUTC once the disk is unmounted?

Does it need a -SetLastWriteTime switch when calling the script after making the changes?

@JimMoyle
Copy link
Contributor

JimMoyle commented Feb 15, 2022 via email

@JimMoyle
Copy link
Contributor

JimMoyle commented Feb 15, 2022 via email

@RyanSwanepoel
Copy link

RyanSwanepoel commented Mar 17, 2022

Thanks so much Jim, finally got some time to get it working with the changes in the Dev branch.

One quick thought, with the "DeleteOlderThan" option it is only removing the disk itself and leaving behind the meta file and folder structure. Is this by design?

Can you see any issue with remove the entire folder? so for example changing from Remove-Item $Disk.FullName to Remove-Item $Disk.DirectoryName as this would clean up the entire profile (Disks and meta).

@lordjeb
Copy link
Member

lordjeb commented Mar 17, 2022 via email

@RyanSwanepoel
Copy link

@lordjeb Agreed but while doing some testing today when the script deletes the disk it leaves behind the meta file, we are currently using CC in our environment. What I was thinking to do was have the same checks against it as the disk in terms of validating the last write time and if its less than your set amount of days remove it. An additional check could also be to make sure the profile folder is empty before removing.

@JimMoyle
Copy link
Contributor

JimMoyle commented Mar 17, 2022 via email

@ASHR4
Copy link

ASHR4 commented May 18, 2022

Can/Has this been implemented?

@amirjs
Copy link

amirjs commented Jul 26, 2022

As MrFly said above, both lastaccesstime and lastwritetime have to be reset after the disk is dismounted. otherwise, the olderthan logic which picks the "maximum" between the two dates will always pick up the most recent i.e. LastAccessTime over LastWriteTime.

I also had to feed each call to DisMount-FslDisk function with these switches. i.e.
DisMount-FslDisk -SetLastWriteTime $lastWriteTime -SetLastAccessTime $lastAccessTime

Great work and effort Jim - thanks much

@beggsy23
Copy link

Would anyone mind sharing the full script that works for this, please?
Along with the 'switches' used to run the script? Thanks in advance.

@ashwin2101
Copy link

Hi can someone please share the script? :)

@Jordyvdp
Copy link

Jordyvdp commented Nov 2, 2023

Thanks so much Jim, finally got some time to get it working with the changes in the Dev branch.

One quick thought, with the "DeleteOlderThan" option it is only removing the disk itself and leaving behind the meta file and folder structure. Is this by design?

Can you see any issue with remove the entire folder? so for example changing from Remove-Item $Disk.FullName to Remove-Item $Disk.DirectoryName as this would clean up the entire profile (Disks and meta).

Would it be possible to share the changes you made? I made the changes from the dev branch but the Date modified still changes.

Thank you in advance!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests