Powershell Set-ADUSER (Email Results)

Andy Weinstock 1 Reputation point
2022-02-08T18:38:19.397+00:00

Hi,

I have a PowerShell script that runs daily and imports a CSV with user information, then uses "set-aduser" to update AD user information with the information from the CSV. However I would like to know if it's possible to email the results of the set-aduser changes?

Ideally the email would provide a list of only the users who had information updated, and indicate which field was updated (and ideally, with what new information). If no users had information updated it would just indicate that nothing was updated.

Is this possible?

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,359 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,036 Reputation points MVP
    2022-02-08T18:57:02.017+00:00

    Hi @Andy Weinstock ,

    in general it should be possible.

    But without knowing your script it's hard/impossible to say "how". Also it might be helpful to get an idea how much users we are talking about and how much expected updates on user information. This numbers might influencing the size of the email or log-file with all the details.
    Do you really read through the details in the email/log-file every day?

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Andy Weinstock 1 Reputation point
    2022-02-08T19:32:41.91+00:00

    Script:

    Run Updates to AD

    $ADUsers = Import-csv "C:\Scripts\AD\ADUpdate\ADUpdate.csv"
    ForEach ($User in $ADUsers)
    {
    $Username = $User.username
    $StreetAddress = $User.streetaddress
    $City = $User.city
    $PostalCode = $User.postalcode
    $State = $User.state
    $Country = $User.country
    $Department = $User.department
    $Title = $User.jobtitle
    $Office = $User.location
    $Company = $User.company
    $EmployeeID = $User.associateID
    $Manager = $User.managername
    $OfficePhone = $User.telephone
    $MobilePhone = $User.mobilephone

    if (Get-ADUser -F {SamAccountName -eq $Username})
    {
    set-aduser -Identity $Username -EmployeeID $EmployeeID
    set-aduser -Identity $Username -Postalcode $Postalcode
    set-aduser -Identity $Username -State $State
    set-aduser -Identity $Username -City $City
    set-aduser -Identity $Username -Department $Department
    set-aduser -Identity $Username -Title $Title
    set-aduser -Identity $Username -Office $Office
    set-aduser -Identity $Username -Company $Company
    set-aduser -Identity $Username -StreetAddress $StreetAddress
    set-aduser -Identity $Username -Manager $Manager
    set-aduser -Identity $Username -OfficePhone $OfficePhone
    set-aduser -Identity $Username -MobilePhone $MobilePhone
    if ($Country -eq "USA - UNITED STATES")
    {
    set-aduser -Identity $Username -Replace @{c="US";co="United States";countrycode=840}
    }
    if ($Country -eq "CA - CANADA")
    {
    set-aduser -Identity $Username -Replace @{c="CA";co="Canada";countrycode=124}
    }
    }

    0 comments No comments

  3. Rich Matheisen 44,776 Reputation points
    2022-02-08T19:53:48.073+00:00

    This is just a brief ecample of one way that you might do this:

    $props = "a","b","c"     # the properties you want to monitor
    $ub = Get-AdUser -Identity "x" -Properties $props -Server MyDC1
    $ua = Set-AdUser -Identity "x" -Properties $props -Server MyDC1
    $props |
        ForEach-Object{
            if ($ub.$_ -ne $ua.$_){
                # property value before change and current value differs after change
            }
            else{
                # no change in selected properties
            }
        }
    

    Using ALL the properties of a user wouldn't be practical. There are properties that might change between the Get and Set user, and there are many properties that are of no interest to you in the context of reporting only the changes you're making.

    0 comments No comments