Solved

Powershell - Do While processes exist

Posted on 2014-12-11
8
1,436 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 28

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 28

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 82

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 28

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 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

17 Experts available now in Live!

Get 1:1 Help Now