Home > Blockchain >  How do I create a loop based on file size in power shell
How do I create a loop based on file size in power shell

Time:02-06

I am working with intune and PowerShell and I basically want to run a exe file which downloads 15.2GB / 7932 files for the insulation off the autodesk website and then creates a text file so that intune knows that it's done as I want to delete all the install files with another script later.

The problem is the PowerShell script will run and close before it has finished downloading and intune thinks it is done and the next script tries to install what was downloaded but it is not fully downloaded so it fails.

I have tried to put a wait command but intune will just hang and you will have to restart windows which is something I don't want the users to do.

I am thinking to add a loop so ot checks the file size of the following folder:

C:\Autodesk\{E658F785-6D4D-4B7F-9BEB-C33C8E0027FA}

and once it reaches 15.2GB / 7932 files it goes to the next step and creates the text file.

Below is my current PowerShell script:

Start-Process -NoNewWindow -FilePath "\\arch-syd-fs\EM Setup\Autodesk Recap Custom Install 2023\Source 1 Download\Revit_2023.exe" -ArgumentList "--quiet' " -Wait
New-Item "C:\Temp\Revit 2023" -Type Directory
New-Item -Path "C:\Temp\Revit 2023\Download Done.txt"

CodePudding user response:

Lets break this down into 3 questions

  • How do you check the size of a directory?
  • How do you check the count of files in a directory?
  • How do you make a script wait until these checks reach a certain value?

It turns out you can do the first 2 together

$dirStats = Get-ChildItem -Recurse -Force 'C:\path\to\whatever' | Measure-Object -Sum Length
$size = $dirStats.Sum
$fileCount = $dirStats.Count

Then you can wrap it in a do-until loop (with a delay to keep it from eating all the CPU) to make the script wait until those values reach a certan threshold

do {
    Start-Sleep 5
    $dirStats = Get-ChildItem -Recurse -Force 'C:\path\to\whatever' | Measure-Object -Sum Length
    $size = $dirStats.Sum
    $fileCount = $dirStats.Count
} until( ($size -ge 15.2*1024*1024*1024) -and ($fileCount -ge 7932) )

Note that $size is in bytes, and you might want to make that an -or condition rather than an -and depending on weather you want the script to return after either condition is met or wait for both.

  • Related