Thursday, September 5, 2013

Powershell Techniques : Use While loop for rebooting laptop after 30 seconds with a informative message .




Hi,

I hope you all are doing well, I am in Italy for this week and enjoying Italian coffee.

Anyway.

In few of my scripts, before rebooting the laptop, I use Start-Sleep cmdlet to give a pause for 30-60 seconds.

We can do this easily using Start-Sleep 30.But, it is always good to show informative message on the screen so that user can know what is going on, to do that, I use while loop.

See the code below and it is very simple.


#variable
$second = 30

#While Loop

#while our $second variable is greater then 0
While ($second -gt 0 ) {

            # we are minus 1 with every look
            $second--
           
            # putting laptop sleeping for one seconds
            sleep 1
           
            # just a message
            Write-Warning "Computer will be restated in  $second"
           
            }

Logically this loop will run 30 times, until $seconds value is not come to zero.

Let's run the code and the result is. nice informative message about that laptops will be reboot in 30 seconds.




















Thanks

I hope you may like this. :) 


Thanks
Aman Dhally
clip_image001 clip_image002 clip_image003 clip_image005clip_image007







No comments:

Post a Comment