Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Powershell - Do While processes exist

Posted on 2014-12-11
8
Medium Priority
?
3,805 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

 
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
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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 41

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

Q2 2017 - Latest Malware & Internet Attacks

WatchGuard’s Threat Lab is a group of dedicated threat researchers committed to helping you stay ahead of the bad guys by providing in-depth analysis of the top security threats to your network.  Check out our latest Quarterly Internet Security Report!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 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