Jump to content

Get-ADuser - Exit Script If There Are No Results


Recommended Posts

Posted

Hello,

 

I am writing a script that will send an email notification from data returned from a Get-ADuser command. For example users that are expiring.

 

9 / 10 times there won't be anything returned by the script. But I don't want it to send emails with no data. That will get annoying pretty quickly!

 

Is there a way to exit the script with no further action if there are no results returned from the Get-Aduser command please?

 

Thanks

Posted

Thanks for the reply. Forgive me, I am not a coder by any means so might need some hand holding.

 

If the below was the Get-aduser command, how would that fit in with your logic please?

 

Get-ADUser -Filter * -SearchBase "OU=Finance,OU=UserAccounts,DC=FABRIKAM,DC=COM"

 

Thank you!

Posted
$users = Get-ADUser -filter * -SearchBase "OU=Finance,DC=scraven,DC=internal"

 

if ($users.Count < 1)

{

Send-MailMessage -Blah

}

 

$users = Get-ADUser -filter * -SearchBase "OU=Finance,DC=scraven,DC=internal"

 

if ($users -eq $null)

{

Send-MailMessage -Blah

}

 

 

I'm not sure what happens when no results so you will need one or other of those; I think the former.

 

Peter

  • Thanks 1
Posted

Sorry, both my examples are the wrong way up!

 

Should be:

$users = Get-ADUser -filter * -SearchBase "OU=Finance,DC=scraven,DC=internal"

 

if ($users.Count -ge 1)

{

Send-MailMessage -Blah

}

 

$users = Get-ADUser -filter * -SearchBase "OU=Finance,DC=scraven,DC=internal"

 

if ($users -ne $null)

{

Send-MailMessage -Blah

}

  • Thanks 1
Posted
Sorry, both my examples are the wrong way up!

 

Should be:

 

The second one worked a treat (I didn’t try the first!).

 

Thanks so much. I have made a couple of scripts and will use it more!

 

I am going to have a play with using it for 2 queries (2 domains). That should be fun!

 

Thanks again!

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...