FN-GM Posted April 20, 2023 Posted April 20, 2023 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
howartp Posted April 20, 2023 Posted April 20, 2023 I suspect I'd invert the logic. if($iHaveSomeData) { sendAnEmail() } Then the script will just end itself anyway.
FN-GM Posted April 20, 2023 Author Posted April 20, 2023 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!
howartp Posted April 20, 2023 Posted April 20, 2023 $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 1
FN-GM Posted April 20, 2023 Author Posted April 20, 2023 Thanks - got it! I will give this a shot and report back. Thanks again!
howartp Posted April 20, 2023 Posted April 20, 2023 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 } 1
FN-GM Posted April 21, 2023 Author Posted April 21, 2023 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! 1
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now