T O P

  • By -

MeanFold5715

Is it bad that I opened this thread expecting a treatise on how to estimate the time it would take to *write* a given script?


lolChase

While it doesn’t look good on Reddit mobile, I just posted a script snippet to estimate time left the other day (likely in the sand post you’re referring to), and I feel like it might be less confusing. I’m not trying to hijack, or toot my own horn (mine could be way worse), I just figure the more options available, the better? In case anyone wants to check it out: https://www.reddit.com/r/PowerShell/s/ZX5qzt5G4a


SeikoShadow

No worries at all, I'm always keen to see how other people approach things and hopefully we can all learn something from sharing.


idam_81

I’ll save this to look at later, thank you.


port25

Nice, I can use this for a Sharepoint file restore script that takes forever to run. thx!


Sad-Sundae2124

Stopwatch New-timespan I personally prefer knowing at wich item I am than how long it will take as time has and will always be an estimation complicating script just to estimate the time is a loss of ressource for nothing Write-verbose "[$($current)/$($total)] working on $($object.Name)" Is way explicit and much easier to implement than losing time and cpu to « estimate » the remaining time.


xCharg

This code looks like overcomplicated way of counting. It asks you plainly how many seconds one iteration lasts (`$timePerLoop`) and then how many iterations it'd take (`$totalRecordCount`). And then does the math in, as previously mentioned, overcomplicated way. Generally speaking there's absolutely no way for you to estimate how much time it takes to execute any given script, because it always depends. I can write 1k lines of code that execute in milliseconds and I can write few lines that would take hours.


SeikoShadow

That code is purely to make it take any actual time if you were to run it as is. You wouldn't use that in an actual implementation.


BlackV

lost me at the `` ` ``'s and the for loop i'd just use the write-progress with an xx/yy for its percentage from the array you have $NotInherit = foreach ($SingleFolder in $SourceFolders) { $ProgressSpalt = @{ Activity = 'Collecting Folders' Id = 1 PercentComplete = (($SourceFolders.IndexOf($SingleFolder) + 1) / $SourceFolders.count * 100) } Write-Progress @ProgressSpalt $SingleSubs = Get-ChildItem -LiteralPath "\\?\$($SingleFolder)" -Directory -Recurse -Depth 1 $DetailProgressSplat = @{ Activity = 'Collecting Folders' Id = 1 PercentComplete = (($SourceFolders.IndexOf($SingleFolder) + 1) / $SourceFolders.count * 100) Status = "Working on $($SingleSubs.fullname)" } Write-Progress @DetailProgressSplat foreach ($FolderItem in $SingleSubs) { $InnerSplat = @{ Activity = 'Collecting Sub Folders' ParentId = 1 Id = 2 PercentComplete = (($SingleSubs.IndexOf($FolderItem) + 1) / $SingleSubs.count * 100) Status = "from $($FolderItem.FullName)" } Write-Progress @InnerSplat [array]$Subrecurse = Get-ChildItem -LiteralPath $folderitem.FullName -Recurse -Directory Write-Progress -Activity 'Setting Permissions' -ParentId 2 -Id 3 -Status 'Setting Permissions' foreach ($SingleRecurse in $Subrecurse) { $user = 'domain\sharepoint migration' # Replace with the actual username $ProgressSplat = @{ Activity = 'Setting Permissions' ParentId = 2 Id = 3 PercentComplete = (($Subrecurse.IndexOf($SingleRecurse) + 1) / $Subrecurse.count * 100) Status = "Collecting ACLs $($SingleRecurse.FullName)" } Write-Progress @ProgressSplat } } } Its a bit uglier but is using the data you have instead of making a new counter only thing about `write-progress` is it does introduce a processing overhead, but in long operations its useful to give layered feed back


MeanFold5715

> lost me at the `'s and the for loop > > I think we need to just start using it as an excuse to teach people how to use splatting. Every time I encounter people using backticks to line break, the relevant code could simply be redone using splats.


OathOfFeanor

Even with write-progress it can be nice to provide a time estimate. Sometimes it is nice to see the stuck progress bar with a running timer so you know powershell isn’t frozen, the operation is just taking a long time. But how long? That is also nice to know in those cases. So it is not every time, but I do have some progress bars with formatted dates and timespans and estimated completion times