Shawn ricshawnawic
Shawn ricshawnawic

Reputation: 157

Progress bar in Power shell script possible?

Im curious about having a progress bar for a function i've made, however i've been trying for the past hour of implementing it into my code but no luck....

Is it even possible , What my program does it reads a list of svr names and then basically invokes a function to grab information from a svr and write to file so i have raw data logs, however i get tired of looking at a blank screen so i wanted to add some sort of indicator .. :( but no luck ..

Would really appreciate some comments thank you all :)

Clear-Host

$serverList = Get-Content C:\RServerList.txt

$creds = Get-Credential

function Build_Directories
{
   foreach ($s in $serverList)
   {
     New-Item -ItemType directory -Path C:\Data\$s
    }
 }


function xecute_eLogs
{
    $i = 1
    foreach ($s in $serverList)
    {
        Invoke-Command -ComputerName $s -ScriptBlock {Get-EventLog -LogName security -EntryType "Error","Warning"} -credential $creds | Out-File "C:\Data\$s\sysElogs.txt"
        Write-Progress -Activity "Retrieving data from $s which is $i out of $($serverList.Count)" ` -percentComplete  ($i++*100/$serverList.Count)

    }
 }



Build_Directories
xecute_eLogs

Upvotes: 1

Views: 1420

Answers (1)

Micky Balladelli
Micky Balladelli

Reputation: 10011

You can use Write-Progress

here is an example:

$max = 25
for ($i = 1; $i -le $max; $i++)
{
     Write-Progress -Activity "Counting $i until $max" `
        -percentComplete  ($i*100/$max)
     sleep -Seconds 1
}

EDIT Taking into consideration comment about status.

Adapted to your script it would look like this:

$i = 1
foreach ($s in $serverList)
{
     Invoke-Command -ComputerName $s -ScriptBlock {Get-EventLog -LogName system -EntryType "Error","Warning"} -credential $creds | Out-File "C:\Data\$s\sysElogs.txt"

     Write-Progress -Activity "Retrieving data from $s which is $i out of $($serverList.Count)" -percentComplete  ($i++*100/$serverList.Count) -status Running
}

Upvotes: 4

Related Questions