Trying to send notifications about expired users to Slack from Active Directory

Hi all! I’m a complete noob here so forgive my ignorance if something is obvious that I didn’t catch.

The point of the code was to send notifications to the managers in Slack if their users are expiring within 7 days or less in Active Directory. So far with testing and the webhook, I’m able to have the output go from AD to a specific channel but it looks like it’s sending ALL the users in Active Directory and even the ones that have been suspended or haven’t been active for years. I’m also having trouble figuring out how I can specifically send the notifications to the users’ corresponding managers or how I can map it.

The code I have is below and full disclosure I had Chatgpt write this up. Any help on this is much appeciated than you!

# Define the parameters
$daysBeforeExpiration = 7 # Set the number of days before expiration to notify
$slackWebhookUrl = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" # Replace with your Slack Webhook URL
# Get AD users whose accounts are expiring within the threshold
$users = Get-ADUser -Filter * -Properties AccountExpirationDate | Where-Object {
  $_.AccountExpirationDate -ne $null -and $_.AccountExpirationDate -lt (Get-Date).AddDays($daysBeforeExpiration)
}
# Loop through the users and send notifications to Slack
foreach ($user in $users) {
  $userName = $user.SamAccountName
  $expirationDate = $user.AccountExpirationDate
  $managerEmail = (Get-ADUser $userName -Properties Manager).Manager # Assuming the manager info is in AD
  # Get the manager's Slack info (you could use a mapping or directory for this)
  # For now, we assume the manager's Slack user ID or channel is already known.
  $slackManagerId = "@manager" # Replace this with the actual Slack manager ID or channel
  # Prepare Slack message payload
  $payload = @{
    text = " *Action Required:* The contractor account '$userName' is set to expire on $expirationDate. Please review."
    channel = $slackManagerId
  } | ConvertTo-Json
  # Send the message to Slack
  Invoke-RestMethod -Uri $slackWebhookUrl -Method Post -Body $payload -ContentType 'application/json'
}
Write-Host "Notifications sent to Slack."

Ron,
Welcome to the forum. :wave:t3:

Before we continue … could you please go back, edit your question once again and fix the formatting of your code?

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

Guide to Posting Code - Redux <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

We tend to not support AI created code here since a lot of AIs have the tendency to hallucinate not existing stuff.

To use AI tools to generate code for you you should be actually familiar with the language already to validate the code delivered by the AI.

I’d recommend to split your task into different sub-task. First get the users you’re looking for from your AD. Then continue to the next step … at the end you can combine all steps to the solution you’re looking for.

2 Likes

Hey Olaf, thank you so much for the tips! I completely understand about not supporting AI code. I’ll try and break down the tasks as you said and go from there. Thank you!

Hi Olaf thank you for your response! Ok it looks a lot better after the formatting link you posted, Honestly I was wondering why it looked completely different from when I originally posted this. As I understand it, AI code is not supported and if that’s so I completely understand and can delete this post. Thanks again!

It is perfectly fine if you want to use AI to get some help but as I said before you should already know the language to a certain degree to be able to validate the code.
And - IMHO - it would be quite disrespectful to let others debug code you didn’t even write yourself. Therefor I really appreciate that you disclosed that you used AI.
Thank you.

@smashcakes I totally understand using AI to give you a first draft for something you don’t yet understand. I find it really helpful, even with years of PowerShell experience, to get a draft of something from AI.

Something that might help you along is to have the AI explain the code it wrote to you. I find that a good learning exercise. Sometimes AI finds a new way to accomplish something I’ve always done differently, and it helps me understand it’s reasoning and how the new idea works.

To get back to your code, like @olaf suggested, I’d try running the Get-ADUser command by itself, since it seems like that’s where the issue is - the query is returning more than you expect. Once you get that line of code working correctly, the rest may just fall into place.

Hey folks,

Thank you so much for your replies! I’m going to do what the both of you have said and break down the code in small bites and have each part working one by one first before combining everything as well as educate myself on basic PS so I won’t have to rely on AI code as much.

1 Like

If you’re looking for a good resource for PowerShell basics, I highly recommend ‘Learn PowerShell in a Month of Lunches.’ It’s the book I recommend to anyone looking to get started in PowerShell.

Hi Psdarwin, thank you so much for the recommendation! I just ordered it from Amazon so should be on it’s way!

1 Like