Solved

End a PowerShell script when a query returns no entries

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 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