Skip to main content

Automate All with Powershell

SHOW PROGRESS WHILE PERFORMING TASK


Have you come across task which take huge amount of time to execute on Powershell, but you don’t want your console to look like it’s STUCK. When console shows nothing executing or cursor is not coming to next line you get confused that is my system running fine or has gone into “HUNG STATE” due to script I just ran.
This might be while checking a VM/Remote server to come in PowerOn state or to get details of product installed using WMI query WIN32_Product. Well in such case we try to use START-JOB/ Receive-Job to execute the command so that task runs in background and console gets free for other tasks.
But in many scenarios our next command is dependent of the output we get out from leading command. In such situation, we can’t just execute our script/command with START-JOB and let our console execute later commands. We need to WAIT.
Waiting may take 5 seconds or few minutes. Till the time you seems confused about time being taken without any progress being shown. Well… this scenario makes me go nuts!!!
While sitting one fine day in my office with less work in my bucket and my boss being on leave 😊 I tried to make this following code so that I can see progress in my scripts while waiting for dependable command to execute without looking on same line for more than 15 seconds.

In Following example I have used win32_product class to search for installed application on my server. Usually this takes bit time to populate the result. Here query is executed as job, later wait of 3second is made each tome to check the status of query. But till the result is populated the console is showing "Fetching Details...." rather than stuck console. This logic add look and feel to output on console.

Start-Job -Name JobName -ScriptBlock {Get-WmiObject win32_product} | Out-Null

write-host "Fetching Details" -NoNewline -ForegroundColor Green

do {
    $Pointer = "."
               
        for ($i = 0; $i -lt 1; $i++) {
            Write-Host $Pointer -NoNewline -ForegroundColor Green
            start-sleep -Seconds 1
                for ($j = 0; $j -lt 1; $j++) {
                Write-Host $Pointer -NoNewline -ForegroundColor Green
                start-sleep -Seconds 1
                    for ($k = 0; $k -lt 1; $k++) {
                    Write-Host $Pointer -NoNewline -ForegroundColor Green
                    start-sleep -Seconds 1
                    }
                }
        }

    }until ((get-job JobName).State -eq "Completed" )

Receive-Job JobName


OUTPUT
------









IdentifyingNumber : {C45340-E73F-4345F-ACD7-ey5435124C}
Name              : ***_I**_Internal_Use_Editable
Vendor            : ****************
Version           : 1.0.0.0
Caption           : ***_***_Internal_Use_Editable


This can be further modified as below. In here, the output on screen will be cleared after each "Fetching Details..." Not more than 3 dots will be shown, after that it will repeat


Start-Job -Name JobName -ScriptBlock {Get-WmiObject win32_product| Out-Null


do {
    Cls
    write-host "Fetching Details" -NoNewline -ForegroundColor Green
    $Pointer = "."
                
        for ($i = 0$i -lt 1$i++) {
            Write-Host $Pointer -NoNewline -ForegroundColor Green
            start-sleep -Seconds 1
                for ($j = 0$j -lt 1$j++) {
                Write-Host $Pointer -NoNewline -ForegroundColor Green
                start-sleep -Seconds 1
                    for ($k = 0$k -lt 1$k++) {
                    Write-Host $Pointer -NoNewline -ForegroundColor Green
                    start-sleep -Seconds 1
                    }
                }
        }

    }until ((get-job JobName).State -eq "Completed" )


Receive-Job JobName 

OUTPUT
------





IdentifyingNumber : {C45340-E73F-4345F-ACD7-ey5435124C}
Name              : ***_I**_Internal_Use_Editable
Vendor            : ****************
Version           : 1.0.0.0
Caption           : ***_***_Internal_Use_Editable

Comments

  1. This removes the blank cursor without any info to looks what ever message you want to print

    ReplyDelete

Post a Comment