FSLogix / Invoke-FslShrinkDisk

This script will shrink a FSLogix Disk to its minimum possible size
MIT License
155 stars 54 forks source link

DeleteOlderThanDays #31

Open AlfredHSwe opened 3 years ago

AlfredHSwe commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 2 years ago

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 commented 2 years ago

yeah, or ntuser.dat or something like that.

RyanSwanepoel commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

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 commented 2 years ago

If you look into the development branch, I've written code there to account fo this which you can use.

Jim

On Fri, 11 Feb 2022 at 15:30, Ryan Swanepoel @.***> wrote:

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.

— Reply to this email directly, view it on GitHub https://github.com/FSLogix/Invoke-FslShrinkDisk/issues/31#issuecomment-1036334544, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE365O6RPPLDTJ4CSR3DIMDU2UTQPANCNFSM4V5OEENQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

RyanSwanepoel commented 2 years ago

Hi Jim thanks for the response!

Am I correct in thinking I would just need to add the changes in commit (5dc8aa71a55fed418ab1a924ff01f96c1aee9d5b) 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 commented 2 years ago

Yep that should do it.

On Tue, 15 Feb 2022 at 09:54, Ryan Swanepoel @.***> wrote:

Hi Jim thanks for the response!

Am I correct in thinking I would just need to add the changes in commit ( 5dc8aa7 https://github.com/FSLogix/Invoke-FslShrinkDisk/commit/5dc8aa71a55fed418ab1a924ff01f96c1aee9d5b) 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?

— Reply to this email directly, view it on GitHub https://github.com/FSLogix/Invoke-FslShrinkDisk/issues/31#issuecomment-1040072125, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE365OZOIFRYPEJRLEGLZWLU3IPGJANCNFSM4V5OEENQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

JimMoyle commented 2 years ago

I decided against the addition of a switch for the feature for reasons of simplicity, but feel free to put it in your script :)

Jim

On Tue, 15 Feb 2022 at 17:11, Jim Moyle @.***> wrote:

Yep that should do it.

On Tue, 15 Feb 2022 at 09:54, Ryan Swanepoel @.***> wrote:

Hi Jim thanks for the response!

Am I correct in thinking I would just need to add the changes in commit ( 5dc8aa7 https://github.com/FSLogix/Invoke-FslShrinkDisk/commit/5dc8aa71a55fed418ab1a924ff01f96c1aee9d5b) 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?

— Reply to this email directly, view it on GitHub https://github.com/FSLogix/Invoke-FslShrinkDisk/issues/31#issuecomment-1040072125, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE365OZOIFRYPEJRLEGLZWLU3IPGJANCNFSM4V5OEENQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

RyanSwanepoel commented 2 years ago

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 commented 2 years ago

Many configurations will have Profiles and ODFC container vhds in the same directory, so I would not think it is always safe to just delete the directory. The meta file is a more recent addition in non-ccd configurations, and should be cleaned up when the vhd is cleaned up.

From: Ryan Swanepoel @.> Sent: Thursday, March 17, 2022 5:06 AM To: FSLogix/Invoke-FslShrinkDisk @.> Cc: Subscribed @.***> Subject: Re: [FSLogix/Invoke-FslShrinkDisk] DeleteOlderThanDays (#31)

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.Path as this would clean up the entire profile (Disks and meta).

— Reply to this email directly, view it on GitHub https://github.com/FSLogix/Invoke-FslShrinkDisk/issues/31#issuecomment-1070796398 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AB6ERMLIMTPDWNG5L244T3LVAMGZ7ANCNFSM4V5OEENQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub . You are receiving this because you are subscribed to this thread. https://github.com/notifications/beacon/AB6ERMLD64FYOBU7YQEDBDTVAMGZ7A5CNFSM4V5OEEN2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOH7JQ43Q.gif Message ID: @. @.> >

RyanSwanepoel commented 2 years ago

@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 commented 2 years ago

Nope, can't see an issue with removing the folder, it's a good idea I should implement

Jim

On Thu, 17 Mar 2022 at 11:06, Ryan Swanepoel @.***> wrote:

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.Path as this would clean up the entire profile (Disks and meta).

— Reply to this email directly, view it on GitHub https://github.com/FSLogix/Invoke-FslShrinkDisk/issues/31#issuecomment-1070796398, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE365O7NMWWJLXKKWJGH3ZDVAMGZ7ANCNFSM4V5OEENQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you commented.Message ID: @.***>

ASHR4 commented 2 years ago

Can/Has this been implemented?

amirjs commented 2 years ago

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 commented 1 year ago

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 commented 1 year ago

Hi can someone please share the script? :)

Jordyvdp commented 11 months ago

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!