Solved

Powershell - Do While processes exist

Posted on 2014-12-11
8
2,334 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 84

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

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 …
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
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…

749 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