Thursday, December 13, 2012

Loops: Do While in PowerShell.

Hi,

As i mentioned earlier that i am very busy in the month on October and November that’s why i was not able to write any article on Powershell, but i am little free now and will try to publish articles whenever i get free time.

Today one of my fried call me and he was having some problem understanding DO WHILE loop, I explained the working of the loop over the phone to him and i thought it is worth writing a post on it.

the working of do while loop is simple.

do { code block }
while ( condition )

for example.

do { Dancing }
while ( music is playing )

You got an idea ? isnt ?

That means do  something till while condition is true. Like in above not-IT example do the Dancing till the Music is Playing Smile 

In IT world we can use it in a various ways . Lets try this ….

  • Open Notepad

13-12-2012 15-18-38

and type the below in the powershell console.

  • Do { Ping google.com } while ( Get-Process Notepad | Select-Object  -Property Responding )

what is happening?

Powershell Will Ping Google.Com till the notepad windows is open.

You can notice our Notepad windows is open and we have constantly pinging Google.com and after 4 Pings it starts again. 

13-12-2012 15-23-46

I hope it is little bit clear now Smile 

Thanks

Aman Dhally

join aman on facebook Join aman on Linkedin follow aman on Twitter

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.