Solved

End a PowerShell script when a query returns no entries

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

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

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
In this video we show how to create a Distribution Group 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 Recipients >>…
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …

746 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

10 Experts available now in Live!

Get 1:1 Help Now