Skip to content

Instantly share code, notes, and snippets.

@mirontoli
Created September 21, 2020 19:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirontoli/346ac564930bb0834bad13d6ea805734 to your computer and use it in GitHub Desktop.
Save mirontoli/346ac564930bb0834bad13d6ea805734 to your computer and use it in GitHub Desktop.
# This is just an example for time estimations in write-progress,
# though a simplified scenario
$sitesBareMinimum = Get-SPOSite -Limit All
$starttime = Get-Date
$count = 0 # kind of an index, counter
$total = $sitesBareMinimum.Count
$sites = $sitesBareMinimum | ForEach-Object {
$site = $_
$estimation = ""
$now = Get-Date
if ($count -gt 0) { # noone wants a DividedByZeroException :)
$elapsed = $now - $starttime # how much time has been spent
$average = $elapsed.TotalSeconds / $count # how many seconds per site
$totalSecondsToGo = ($total - $count) * $average # seconds left
$span = New-TimeSpan -Seconds $totalSecondsToGo # time left
$estimatedCompletion = $now + $span # when it will be complete
$estimation = $estimatedCompletion.ToString() # readable estimation
}
$count++
$percent = 100 * $count / $total # percentage complete
$status = "#{0:d5} of $total. Est:d $estimation. $($site.URL)" -f $count # aggregated status message
Write-Progress -Activity "Getting information for " -Status $status -PercentComplete $percent
$siteWithMoreInfo = Get-SPOSite -Identity $site.URL # the actual time consuming operation
$siteWithMoreInfo # return the site with more information
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment