Solved

Powershell - Do While processes exist

Posted on 2014-12-11
8
1,642 Views
Last Modified: 2014-12-12
Hi guys, how do I go about creating a Do...While loop which checks for the existence of multiple processes?

Not sure of the syntax, but something like:

Do
{
"Processes exist"
$proc = Get-Process
start-sleep 2
} While ($proc.name -contains 'process1') or ($proc.name -contains 'process2') or ($proc.name -contains 'process3')
0
Comment
Question by:amaru96
8 Comments
 
LVL 29

Expert Comment

by:becraig
ID: 40495504
You need to run get process in your while loop or else you're stuck with the snapshot from your do statement
0
 
LVL 1

Author Comment

by:amaru96
ID: 40495563
I do have "get process" within the loop.

Do
{
"Processes exist"
$proc = Get-Process
start-sleep 2
} While ($proc.name -contains 'process1') or ($proc.name -contains 'process2') or ($proc.name -contains 'process3')

My question is around the multiple while condition. What is the syntax? At the moment I get an error:

or : The term 'or' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I have also tried -or but get the same error. I don't know how to write the multiple while command.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40495564
Or must be -or
0
 
LVL 1

Author Comment

by:amaru96
ID: 40495567
Dude, are you reading my comments?

I have also tried -or but get the same error. I don't know how to write the multiple while command.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 40495568
No need for a condition concatentation like this.
$ProcessList = @(
	"notepad",
	"calc"
)
Do {
	$ProcessesFound = Get-Process | ? {$ProcessList -contains $_.Name} | Select-Object -ExpandProperty Name
	If ($ProcessesFound) {
		"Still running: $($ProcessesFound -join ', ')" | Write-Host
		Start-Sleep -Seconds 2
	}
} Until (!$ProcessesFound)

Open in new window

0
 
LVL 1

Author Comment

by:amaru96
ID: 40495579
Excellent, thanks oBdA, works perfectly!
0
 
LVL 29

Expert Comment

by:becraig
ID: 40495581
Did not see the last line (on my phone)

However odba provided a code example for my suggestion that did not seem to post.  do until
0
 
LVL 39

Expert Comment

by:footech
ID: 40495916
BTW, here's how you could check for multiple conditions.  The inner parentheses aren't actually needed, it's the outer ones that are the key.
While (($proc.name -contains 'process1') -or ($proc.name -contains 'process2') -or ($proc.name -contains 'process3'))

Open in new window

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now