?
Solved

End a PowerShell script when a query returns no entries

Posted on 2014-07-24
2
Medium Priority
?
546 Views
Last Modified: 2014-07-24
A few day ago, an Expert, Justin Yeung, fixed a script for me to check for a null value in the AD attribute msExchVersion and then perform an action on users found. I'll paste the value below. I'm wondering how I can terminate the script if the Get-ADUser returns no results. This would prevent the rest of the script from running. Any help is appreciated.
Import-Module ActiveDirectory
Add-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010

$Users = Get-ADUser -LDAPFilter "(!(msExchVersion=*))" -resultSetSize $null -searchbase "OU=TEST,OU=TestUsers,OU=VOIP,OU=User Accounts,DC=domain,DC=priv"
foreach ($User in $Users)
{
Set-aduser $User.SamAccountName -add @{msExchVersion="44220983382016"}
Start-sleep -second 15
Get-Mailbox $User.DistinguishedName -IgnoreDefaultScope | Update-Recipient
}

Open in new window


Any help is appreciated.
0
Comment
Question by:hcca
[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
2 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40217498
Import-Module ActiveDirectory
Add-PSSnapin -Name Microsoft.Exchange.Management.PowerShell.E2010

$Users = Get-ADUser -LDAPFilter "(!(msExchVersion=*))" -resultSetSize $null -searchbase "OU=TEST,OU=TestUsers,OU=VOIP,OU=User Accounts,DC=domain,DC=priv"
if ($users -ne $null -and $users -ne "") 
{

foreach ($User in $Users)
{
Set-aduser $User.SamAccountName -add @{msExchVersion="44220983382016"}
Start-sleep -second 15
Get-Mailbox $User.DistinguishedName -IgnoreDefaultScope | Update-Recipient
}
}

else {write-host "No users found... Exiting..." -fore RED;exit; }

Open in new window

0
 

Author Closing Comment

by:hcca
ID: 40217733
Works like a champ. I never thought of the -ne and probably would still have gotten in wrong. I knew it would take an if else pair but could not work it out.
Thank you so much.
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Active Directory can easily get cluttered with unused service, user and computer accounts. In this article, I will show you the way I like to implement ADCleanup..
In this Micro Video tutorial you will learn the basics about Database Availability Groups and How to configure one using a live Exchange Server Environment. The video tutorial explains the basics of the Exchange server Database Availability grou…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

771 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