• Sign in to Neowin Faster!

    Create an account on Neowin to contribute and support the site.

Sign in to follow this  

PowerShell Get-ADUser help

Recommended Posts

storalta    2
Posted (edited)

I'm having some problems with a PowerShell script I'm writing. What the script does is pull events from the event log and send an email to the user. Everything works fine except the portion of the script where it grabs the user. 

$New_GrpUser = $event.Event.EventData.Data[0]."#text"

It grabs the user successfully, but uses the entire CN. 

CN=<redacted>,OU=Users,OU=Accounting,OU=<redacted>,DC=<redacted>,DC=local

I've tried the following with no success.

$New_GrpUser = $event.Event.EventData.Data[0]."#text"
$New_User = Get-ADUser -Filter { cn -eq $New_GrpUser } -Properties name

Can anybody help with getting the Name property from New_GrpUser? Here's the entire script.

 

$CurrTime = (get-date) - (new-timespan -hour 3)
Get-WinEvent -FilterHashtable @{LogName="Security";ID=4728;StartTime=$CurrTime}| Foreach {
$event = [xml]$_.ToXml()
if($event)
{
    $CurrTime = Get-Date $_.TimeCreated -UFormat "%Y-%d-%m %H:%M:%S"
    $New_GrpUser = $event.Event.EventData.Data[0]."#text"
    $New_User = Get-ADUser -Filter { cn -eq $New_GrpUser } -Properties name
    $AD_Group = $event.Event.EventData.Data[2]."#text"
    $AdminWhoAdded = $event.Event.EventData.Data[6]."#text"
    $dc = $event.Event.System.computer

Send-MailMessage -SmtpServer SMTPSERVER -From smtp@example.com -To admin@example.com -Subject "$New_User has been added to the $AD_Group group" -Body "Added by $AdminWhoAdded" -Priority High
}
}

 

Edited by Jason S.
removed PII

Share this post


Link to post
Share on other sites
storalta    2

I was able to get this to work. For those interested, here's the change required.

 

$New_GrpUser = $event.Event.EventData.Data[0]."#text"
$New_GrpUser = $New_GrpUser -replace "cn=", '' -replace "\\", '' -replace ",OU=.*", ''

 

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.