Solved

End a PowerShell script when a query returns no entries

Posted on 2014-07-24
2
457 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

15 Experts available now in Live!

Get 1:1 Help Now