Solved

End a PowerShell script when a query returns no entries

Posted on 2014-07-24
2
448 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
2 Comments
 
LVL 29

Accepted Solution

by:
becraig earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

895 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

19 Experts available now in Live!

Get 1:1 Help Now