FSLogix / Invoke-FslShrinkDisk

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

Where to amend? #48

Closed sgealbhain closed 3 years ago

sgealbhain commented 3 years ago

Hi Jim,

I need to insert some code that removes cached OneDrive files not accessed within the last 30 days. I have the line of code that does this piece, but I'm wondering where to insert it in your script?

The logic would be quite simple.

1) The VHD is mounted 2) The OneDrive function runs 3) Once the files have been removed, the remainder of your script can run to shrink the VHD and unmount.

Thanks in advance!

JimMoyle commented 3 years ago

I'd do it after the '#Initial disk Mount' try/catch block in the optimize onedisk function this appears in two places and is called depending on which version of powershell you are using.

However If you are trying to save space I'd enact storage sense and files on demand rather than doing this. (2019 or Win10)

If you are on 2016 I'd dehydrate the files rather than deleting them you can do this with powershell.

Jim

sgealbhain commented 2 years ago

Hi Jim,

I have been told we can't enable storage sense and must use Powershell for this task. Now I have slotted my code after the '#Initial disk Mount' try/catch block in the optimize onedisk function but in order to run, it needs to access F:\OneDrive. However, I'm not sure how to get code inside the script to reference certain folder paths as there's no drive letter when the disk is mounted. How would I do this?

Thanks, Stephen

JimMoyle commented 2 years ago

I don't use drive letters as I can't guarantee that the user is using less threads than they have letters free so I mount to a temp directory which path you can then access inside the $mount.path variable in Optimize-OneDisk

sgealbhain commented 2 years ago

So when I use $mount.path it gives me a long string rather than a temp drive letter. Like this: Mnt-aaed3d12-f59e-44e1-9aca-14f8210c354

Is there a way to access this or a way to reference the temp drive letter?

JimMoyle commented 2 years ago

I don't use drive letters as if someone is using 30 threads to shrink disks there are only 26 letters in the alphabet and this would obviously cause issues, you can access the drive contents via the path.

Jim

On Tue, 9 Nov 2021 at 11:27, Stephen @.***> wrote:

So when I use $mount.path it gives me a long string rather than a temp drive letter. Like this: Mnt-aaed3d12-f59e-44e1-9aca-14f8210c354

Is there a way to access this or a way to reference the temp drive letter?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/FSLogix/Invoke-FslShrinkDisk/issues/48#issuecomment-964060864, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE365O562RT64GNAMJ75L3LULEAQHANCNFSM5FIY3SJQ . 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.

sgealbhain commented 2 years ago

Makes sense Jim. So I want to access \OneDrive\Contents so how would I specify the full path to that using the temp path? I have tried $path = "$($mount.path)\OneDrive\files\Business1\PSTools" but that doesn't work. If I write out the contents of $mount it gives me

@{Path=C:\Users\stephen\AppData\Local\Temp\FSLogixMnt-db8b3eb5-9915-488d-bae1-bcee96964ee1; DiskNumber=2; ImagePath=E:\fslogix\S-1-5-21-7145671942-14888194-2018343319-7689899_t612\ODFC_t612.VHDX; PartitionNumber=1}

sgealbhain commented 2 years ago

Nevermind I figured it out! Thanks Jim

sgealbhain commented 2 years ago

I've added in this code in the Optimtize-OneDisk function. It works as it should but with some vhds I notice I'm getting the below error.

Get-RunspaceData : Could not find a part of the path

`#Initial disk Mount try { $mount = Mount-FslDisk -Path $Disk.FullName -TimeOut 30 -PassThru -ErrorAction Stop } catch { $err = $error[0] Write-VhdOutput -DiskState $err -EndTime (Get-Date) return }

    ###################################################
    $limit = (Get-Date).AddDays(-30)
    $path = "$($mount.path)\OneDrive\files\Business1\"
    New-PSDrive "X" -PSProvider FileSystem -Root "$path"
    Start-Transcript -Path "X:\SCRIPT_Transcript.txt" -NoClobber
    # Delete files older than the $limit.
    $OneDriveFiles = Get-ChildItem -Path "X:\" -Recurse -Force | Where-Object { (!$_.PSIsContainer -and $_.LastAccessTime -gt $limit) -and $_.Attributes -notmatch "sparse" } # | Remove-Item -Force
    $OneDriveFiles
    Write-Host $mount -f Yellow

    Stop-Transcript -Path "X:\SCRIPT_Transcript.txt" -NoClobber

    ###################################################

    #Grabbing partition info can fail when the client is under heavy load so.......`
JimMoyle commented 2 years ago

OK so remember I don't approve of you doing this.......

But the disk is already mounted to the path, why are you trying to double mount it to a drive letter? I'd also create variables which will be unique to you unless you can be sure I have not used '$path' elsewhere.

The script is running in a runspace so you won't see write-host it's just slowing everything down and you are writing the transcript to the folder you are reading.

I'd do something more like this:

    $sgLimit = (Get-Date).AddDays(-30)
    $sgPath = Join-Path $mount.path 'OneDrive\files\Business1'
    Start-Transcript -Path "$env:Temp\SGlog.txt" -NoClobber
    # Delete files older than the $limit.
    Get-ChildItem -Path $sgPath -Recurse -Force | Where-Object { (!$_.PSIsContainer -and $_.LastAccessTime -gt $limit) -and $_.Attributes -notmatch "sparse" } # | Remove-Item -Force
    Stop-Transcript -Path "$env:Temp\SGlog.txt" -NoClobber
sgealbhain commented 2 years ago

Ah yes, let me give this a try! The reason for the double mount is because I thought it was having problems with the the long file path. I'll try out your suggestion. Thanks, Jim!

sgealbhain commented 2 years ago

Getting the same thing with your suggestion also I'm afraid. I'm wondering if its the length of the overall path? Maybe not?

Get-RunspaceData : Could not find a part of the path 'C:\Users\AutoAdmin\AppData\Local\Temp\FSLogixMnt-79ba688772f-40f3-8edc-6e8f5c3bc\OneDrive\files\Business1_Existing\320Drive\Supp\Ben \CompleteViewSupport\jakarta-jmeter-2.3.2\docs\api\org\apache\jmeter\protocol\ldap'. At C:\Scripts\Invoke-FslShrinkDisk-master\Invoke-FslShrinkDisk-master\Invoke-FslShrinkDisk.ps1:758 char:13

JimMoyle commented 2 years ago

So to debug a runspace you need to put wait-debugger in the script and attach to the thread

Jim

On Wed, 17 Nov 2021 at 15:22, Stephen @.***> wrote:

Getting the same thing with your suggestion also I'm afraid. I'm wondering if its the length of the overall path? Maybe not?

Get-RunspaceData : Could not find a part of the path

'C:\Users\AutoAdmin\AppData\Local\Temp\FSLogixMnt-79ba688772f-40f3-8edc-6e8f5c3bc\OneDrive\files\Business1_Existing\320Drive\Supp\Ben

\CompleteViewSupport\jakarta-jmeter-2.3.2\docs\api\org\apache\jmeter\protocol\ldap'. At C:\Scripts\Invoke-FslShrinkDisk-master\Invoke-FslShrinkDisk-master\Invoke-FslShrinkDisk.ps1:758 char:13

-

       Get-RunspaceData -wait

-

       ~~~~~~~~~~~~~~~~~~~~~~
  • CategoryInfo : ReadError: (C:\Users\Automa...r\protocol\ldap:String) [Write-Error], DirectoryNotFoundException
    • FullyQualifiedErrorId : DirIOError,Get-RunspaceData

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/FSLogix/Invoke-FslShrinkDisk/issues/48#issuecomment-971685197, or unsubscribe https://github.com/notifications/unsubscribe-auth/AE365O26B65SYEH7KTWQMN3UMPCE3ANCNFSM5FIY3SJQ . 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.