?
Solved

Powershell - Do While processes exist

Posted on 2014-12-11
8
Medium Priority
?
3,313 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 40

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

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

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

765 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