Share via

Powershell to automate extension attribute

Ramki 816 Reputation points
2022-04-08T10:29:07.533+00:00

Powershell to automate extension attribute

Windows for business | Windows Server | User experience | PowerShell

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2022-04-11T18:45:16.887+00:00

Here's an example of extracting a table from an HTML document and turning the table into a series of PSCustomObjects. The HTML document is the one you get in the email body.

# create a phony user
$uc =   [PSCustomObject]@{
            DisplayName = "DispName"
            SamAccountName = "accountname"
            UserPrincipalname = "UPN"
            City = "Paris"
            Co = "fr"
            ExtensionAttribute5 = "someattributevalue"
        }

$a = 
@"
<style>
BODY{background-color:white;}
TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;border-spacing: 15px;width:70%;}
TH{border-width: 2px;padding: 8px;border-style: solid;border-color: black;background-color:#d5ebf5;text-align:center;}
TD{border-width: 2px;padding: 8px;border-style: solid;border-color: black;background-color:#f8faf6;text-align:center;}
</style>
"@
$pre =
@"
<h3>Please Remove the BYE String Alone for the below users from ExtensionAttrbute 5 </h3></br>
"@

# combine all that into an HTML document
$b = $uc | ConvertTo-Html -head $a -PreContent $pre

$h = $b -join ""    # Get the HTML into a single string


# Parse the HTML
$HTML = New-Object -Com "HTMLFile"
try {
    $html.IHTMLDocument2_write($h)
}
catch {
    $encoded = [System.Text.Encoding]::Unicode.GetBytes($h)
    $html.write($encoded)
}

# Extract the one (and only) table lement from the HTML
$tables = $html.getElementsByTagName("TABLE")
$table = $tables[0]     # NOTE: there's only ONE table in the HTML doc
$titles = @()
$rows = @($table.Rows)
## Go through all of the rows in the table (which includes a header row, if one is present)
foreach($row in $rows)
{
    $cells = @($row.Cells)

    ## If we've found a table header, remember its titles
    if($cells[0].tagName -eq "TH")
    {
        $titles = @($cells | ForEach-Object { ("" + $_.InnerText).Trim() })
        continue
    }

    ## If we haven't found any table headers, make up names "P1", "P2", etc.
    if(-not $titles)
    {
        $titles = @(1..($cells.Count + 2) | ForEach-Object { "P$_" })
    }

    ## Now go through the cells in the the row. For each, try to find the
    ## title that represents that column and create a hashtable mapping those
    ## titles to content
    $resultObject = [Ordered] @{}
    for($counter = 0; $counter -lt $cells.Count; $counter++)
    {
        $title = $titles[$counter]
        if(-not $title) { continue }  

        $resultObject[$title] = ("" + $cells[$counter].InnerText).Trim()
    }

    ## And finally cast that hashtable to a PSCustomObject
    [PSCustomObject] $resultObject
}

I'm anticipating your next question to be how to read the email and get the message body. For that, create a NEW post.

BUT . . . if you know how the e-mail is constructed, WHY can't you simple have the script that creates the e-mail create a CSV file instead? That's a lot easier to deal with than what you'll go through to get at the message content!

Was this answer helpful?


3 additional answers

Sort by: Most helpful
  1. Ramki 816 Reputation points
    2022-04-11T06:14:29.813+00:00

    somme issue in portal let me update here
    @Rich Matheisen : Thanks a lot ......but the challenge is here for fully automation. here is the script will noitify us

    $a = "<style>"
    $a = $a + "BODY{background-color:white;}"
    $a = $a + "TABLE{border-width: 3px;border-style: solid;border-color: black;border-collapse: collapse;border-spacing: 15px;width:70%;}"
    $a = $a + "TH{border-width: 2px;padding: 8px;border-style: solid;border-color: black;background-color:#d5ebf5;text-align:center;}"
    $a = $a + "TD{border-width: 2px;padding: 8px;border-style: solid;border-color: black;background-color:#f8faf6;text-align:center;}"
    $a = $a + "</style>"
    $uc = Get-ADUser -Filter {msExchExtensionAttribute5 -like "BYE"} -Properties * | Select DisplayName, SamAccountName, UserPrincipalname, City, Co, ExtensionAttribute5

    $body = (
    $uc | Sort-Object UserPrincipalname | Select DisplayName,SamAccountName,UserPrincipalname, City, Co, ExtensionAttribute5 | ConvertTo-Html -head $a -PreContent "<h3>Please Remove the BYE String Alone for the below users from ExtensionAttrbute 5 </h3></br>"
    )

    Send-mailmessage -to "@cloudmonkeys.xyx" -smtpserver "smtp.office365.com" -subject " REMOVE the BYE" -From "@cloudmonkeys.xyx".com" -Body ($Body | Out-String) -BodyAsHtml

    The challenging part is to me. first how can i use the variable $uc = who is the list of users or one user to remove the BYE from EA5 without putting into the txt file manually

    the continuation of above script to remove the the BYE string from EA5 which is available in $uc and follwoed by it should send the email notification to $uC

    Was this answer helpful?

    0 comments No comments

  2. Rich Matheisen 48,116 Reputation points
    2022-04-10T14:48:59.133+00:00

    Something like this may work:

    $stringtoremove = "bye"
    Get-Content UserIdentiy.txt |
        ForEach-Object{
            $uid = $_
            Try{
                Get-ADUser -Identity $_ -Properties extentionattribute5 -ErrorAction STOP |
                $str = $_.extensionattribute5 -replace $stringtoremove
                $_ | SetADUser -Replace @{extensionattribute5 = $str}
            }
            Catch{
                "Failed to Get or Set user identity '$uid'"
            }
        }
    

    There's a unknown, though: What is the content of extentionattribute5? Is it a single string, or is a string that contains either (or both) carriage-return and newline characters? The regular expression may need adjustment to work properly (e.g. to remove a newline character or, maybe one of the "|" characters).

    Was this answer helpful?


  3. Simon Burbery 736 Reputation points
    2022-04-08T12:58:14.39+00:00

    Set-ADUser -Replace @{extensionAttribute1="newvalue"}

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.