Freigeben über


Pure PowerShell Post–Variation From Previous Post: Commented Sample Using PowerShell Progress Bar With Secondary Progress Bar …

#STEPS FOR USING A PROGRESS BAR - With Child Progress Bar...

 

#NOTE: You can copy paste that whole post to an ISE or a NOTEPAD and save it as a .ps1 script - the explanations are put in comments for that purpose :-)

 

 

#STEP 1 - Get your main objects collection you're going to browse and for which you wish to monitor progression - it can be a Get-Item, or an Import-CSV or a manual list for example - $Mylist = "Server1", "Server2", "Server3", "Server4"

$MyListOfObjectsToProcess = "Server1","Server2","Server3","Server4","Server5","Server6"

 

#STEP 2 - We need to know how much  items we will have to process (to calculate the % done later on)

$TotalItems = $MyListOfObjectsToProcess.count

 

 

#STEP 3 - We need to initialize a counter to know what % is done

$Counter = 0

 

#STEP 4 - Process each item (Foreach $Item) of your List Of Objects To Process (in $MyListOfObjectsToProcess)

Foreach ($Item in $MyListOfObjectsToProcess) {

 

 

#STEP 5 - We increment the counter

    #  NOTE 1 : if we initialize the counter to $Counter = 1, then we can increment the counter at the end of the Foreach block - in this example we initialized the counter to $Counter = 0 then we increment it right away to go to "1" first ...

    #  NOTE 2 :Developpers like to start at "0" so usually you'll see counters increment at the end of ForEach-like loops - I'm just not following the sheep here, just because I'm French :-p

    $Counter++

 

 

#STEP 6 - Here is the core : Write-Progress - it comes with 3 mandatory properties : ACTIVITY, STATUS and PERCENTCOMPLETE

    #  ACTIVITY is the title of the progress bar

    #  STATUS is to show on screen which element it is currently processing (Processing item #2 of #20000)

    #  PERCENTCOMPLETE is the progress bar itself and it has to be a percentage (hence the $($Counter / $TotalRecipients * 100) as we calculate the percentage on the fly...

    #  ID is when we intend to use NESTED progress bars - here we give an ID of 1

    Write-Progress -Activity "Processing Items" -Status "Item $Counter of $TotalItems" -PercentComplete $($Counter / $TotalItems * 100) -Id 1

 

 

#YOUR MAIN ROUTINE - Here whatever you want to process on each $Item - that doesn't change anything to the Write-Progress, apart from the time it takes for the progress bar to go to the next % done...

#  In the below example it's just sleeping for 1 second, then outputting each $Item we're processing for the Demo purposes ...

    sleep 1

    Write-Host $Item

 

#STEP 6.1 - YOUR SUB ROUTINE where we want a sub-progress bar

    # Same as before, another counter we start - but this time instead of a Foreach, to vary a bit I'll use a For ($var;$var -gt 10;$var++) { Write-Progress sub-bar, routine, ...} and I'm just storing the max of that Var to process that many times I  want

    $jmax = 10

    For ($j=1;$j -le $jmax;$j++) {

#STEP 6.2 - Here's where we can put a Write-Progress bar, indicating which progress bar was the parent, using PARENTID :

        Write-Progress -Activity "Processing Sub-Items of Items" -Status "Sub-Item $j of $jmax" -PercentComplete $($j / $jmax * 100) -ParentId 1

        Write-host $j

        Sleep 1

`      }

 

}

 

 

 

 

<# Fore more information visit:

 

Powershell progress bar basics :

https://technet.microsoft.com/en-us/library/ff730943.aspx

 

A little bit more with Write-Progress:

https://technet.microsoft.com/en-us/library/2008.03.powershell.aspx

 

#>