Skip to main content

Updating a Extended property based on Group Membership Powershell GUI

Group membership has long been used in directory services to provide a way of giving access to different objects, distribution lists and a variety of other useful things to solve everyday problems in both active directory and Exchange Infrastructures. There are a number of things that you can’t do with Groups in Exchange which can be slightly limiting if you’re trying to solve certain problems in an easy and flexible manner. Exchange provides a whole bunch of extended properties in Active directory that can be used for various things one of the more useful of this is being able to apply a specific proxyaddress based on a specific value set in a extended property via a recipient policy. It can also be useful in Transport Agents and Transport Rules and a number of different application uses.

For example in most mail systems you will have logical grouping of users into distribution lists maybe loosely based on department function etc. If for example your sales department want to use a specific sub domain for a specific promotion they are running let’s call it @worldcup.youdomain.com and you want this to apply to everybody in the sales department (who are in for example a sales distribution list) then this is the script that could help you out.

What this script does is first provides a search mechanism to find a group based on its name using ADSI. Once the group is found you can then view its membership and show any current extended properties that are set. There is an export button so you can export any of the current settings for users that are show in the grid. To use this script you need to select the group you want to affect in the left hand grid select the property you want to set from the combo box and put the value in you want set for all user in the group and hit update.The script will then go through and set the property using ADSI on every member of the group. The script does a nested group query so will affect every member of the group and any members of nested groupd.

The show member’s query will show you all the users that it will affect.

I’ve put a download of this script here the script itself looks like.

[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$form = new-object System.Windows.Forms.form

$repeathashGroup = @{ }
$repeathashUser = @{ }

function Get-member($GroupName){
$Grouppath = "LDAP://" + $GroupName
$groupObj = [ADSI]$Grouppath
foreach($member in $groupObj.Member){
$userPath = "LDAP://" + $member
$UserObj = [ADSI]$userPath
if($UserObj.groupType.Value -eq $null){
if($repeathashUser.ContainsKey($UserObj.distinguishedName.ToString()) -eq $false){
$repeathashUser.add($UserObj.distinguishedName.ToString(),1)
$grTable.Rows.Add($UserObj.DisplayName.ToString(),$UserObj.extensionAttribute1.Value,`
$UserObj.extensionAttribute2.Value,$UserObj.extensionAttribute3.Value,$UserObj.extensionAttribute4.Value,$UserObj.extensionAttribute5.Value,`
$UserObj.extensionAttribute6.Value,$UserObj.extensionAttribute7.Value,$UserObj.extensionAttribute8.Value,$UserObj.extensionAttribute9.Value,`
$UserObj.extensionAttribute10.Value,$UserObj.extensionAttribute11.Value,`
$UserObj.extensionAttribute12.Value,$UserObj.extensionAttribute13.Value,$UserObj.extensionAttribute14.Value,$UserObj.extensionAttribute15.Value)
}

}
else{
if($repeathashGroup.ContainsKey($UserObj.distinguishedName.ToString()) -eq $false){
$repeathashGroup.add($UserObj.distinguishedName.ToString(),1)
Get-member($UserObj.distinguishedName)
}
}
}
$dgDataGrid2.DataSource = $grTable
}

function Update-member($GroupName){
#Warn first
$buttons=[system.windows.forms.messageboxbuttons]::yesno
$return = ""
$return = [system.windows.forms.messagebox]::Show("This will set the selected Extended Properties for all members of the group and nested Groups do you want to proceed","",$buttons)
if ($return -eq [Windows.Forms.DialogResult]::Yes){
$Grouppath = "LDAP://" + $GroupName
$groupObj = [ADSI]$Grouppath
foreach($member in $groupObj.Member){
$userPath = "LDAP://" + $member
$UserObj = [ADSI]$userPath
if($UserObj.groupType.Value -eq $null){
if($repeathashUser.ContainsKey($UserObj.distinguishedName.ToString()) -eq $false){
$propname = $exPropDrop.SelectedItem.ToString()
$repeathashUser.add($UserObj.distinguishedName.ToString(),1)
$UserObj.$propname = $uvtext.Text
$UserObj.SetInfo()
}

}
else{
if($repeathashGroup.ContainsKey($UserObj.distinguishedName.ToString()) -eq $false){
$repeathashGroup.add($UserObj.distinguishedName.ToString(),1)
Update-member($UserObj.distinguishedName)
}
}
}
}
else{
[system.windows.forms.messagebox]::Show("Update Aborted")
}
$grTable.Clear()
$repeathashUser.clear()
$repeathashGroup.clear()
Get-member($msTable.DefaultView[$dgDataGrid.CurrentCell.RowIndex][1])

}

function ExportGrid(){
$exFileName = new-object System.Windows.Forms.saveFileDialog
$exFileName.DefaultExt = "csv"
$exFileName.Filter = "csv files (*.csv)|*.csv"
$exFileName.InitialDirectory = "c:\temp"
$exFileName.ShowHelp = $true
$exFileName.ShowDialog()
if ($exFileName.FileName -ne ""){
$logfile = new-object IO.StreamWriter($exFileName.FileName,$true)
$logfile.WriteLine("MemberName,extensionAttribute1,extensionAttribute2,extensionAttribute3,extensionAttribute4,extensionAttribute5,extensionAttribute6,extensionAttribute7,extensionAttribute8,extensionAttribute9,extensionAttribute10,extensionAttribute11,extensionAttribute12,extensionAttribute13,extensionAttribute14,extensionAttribute15")
foreach($row in $grTable.Rows){
$logfile.WriteLine("`"" + $row[0].ToString() + "`"," + $row[1].ToString() + "," + $row[2].ToString() + "," + $row[3].ToString() + "," + $row[4].ToString()+ "," + $row[5].ToString() + "," + $row[6].ToString() + "," + $row[7].ToString() + "," + $row[8].ToString()`
+ "," + $row[9].ToString() + "," + $row[10].ToString() + "," + $row[11].ToString() + "," + $row[12].ToString() + "," + $row[13].ToString() + "," + $row[14].ToString() + "," + $row[15].ToString())
}
$logfile.Close()
}

}

function SearchfoGroup(){
$msTable.clear()
$root = [ADSI]'LDAP://RootDSE'
if ($rbSearchDWide.Checked -eq $true){
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
}
else{
$dfDefaultRootPath = "LDAP://" + $ouOUNameDrop.SelectedItem.ToString()
}
$dfRoot = [ADSI]$dfDefaultRootPath
$gfGALQueryFilter = "(&(objectClass=group)(displayName=" + $pnProxyAddress.Text + "*))"
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($dfRoot)
if($ouOUCheckBox.Checked -eq $false -band $rbSearchDWide.Checked -eq $false){$dfsearcher.SearchScope = "OneLevel"}
$dfsearcher.Filter = $gfGALQueryFilter
$srSearchResult = $dfsearcher.FindAll()
foreach ($emResult in $srSearchResult) {
$unUserobject = New-Object System.DirectoryServices.directoryentry
$unUserobject = $emResult.GetDirectoryEntry()
$msTable.Rows.Add($unUserobject.DisplayName.ToString(),$unUserobject.distinguishedName.ToString())

}
$dgDataGrid.DataSource = $msTable

}

$msTable = New-Object System.Data.DataTable
$msTable.TableName = "GroupName"
$msTable.Columns.Add("GroupName")
$msTable.Columns.Add("DistinguishedName")

$grTable = New-Object System.Data.DataTable
$grTable.TableName = "Members"
$grTable.Columns.Add("MemberName")
$grTable.Columns.Add("extensionAttribute1")
$grTable.Columns.Add("extensionAttribute2")
$grTable.Columns.Add("extensionAttribute3")
$grTable.Columns.Add("extensionAttribute4")
$grTable.Columns.Add("extensionAttribute5")
$grTable.Columns.Add("extensionAttribute6")
$grTable.Columns.Add("extensionAttribute7")
$grTable.Columns.Add("extensionAttribute8")
$grTable.Columns.Add("extensionAttribute9")
$grTable.Columns.Add("extensionAttribute10")
$grTable.Columns.Add("extensionAttribute11")
$grTable.Columns.Add("extensionAttribute12")
$grTable.Columns.Add("extensionAttribute13")
$grTable.Columns.Add("extensionAttribute14")
$grTable.Columns.Add("extensionAttribute15")



# Add RadioButtons
$rbSearchDWide = new-object System.Windows.Forms.RadioButton
$rbSearchDWide.Location = new-object System.Drawing.Size(20,20)
$rbSearchDWide.size = new-object System.Drawing.Size(150,17)
$rbSearchDWide.Checked = $true
$rbSearchDWide.Text = "Search Domain Wide"
$rbSearchDWide.Add_Click({if ($rbSearchDWide.Checked -eq $true){$ouOUNameDrop.Enabled = $false}})
$form.Controls.Add($rbSearchDWide)

$rbSearchOUWide = new-object System.Windows.Forms.RadioButton
$rbSearchOUWide.Location = new-object System.Drawing.Size(20,60)
$rbSearchOUWide.size = new-object System.Drawing.Size(150,17)
$rbSearchOUWide.Checked = $false
$rbSearchOUWide.Add_Click({if ($rbSearchDWide.Checked -eq $false){$ouOUNameDrop.Enabled = $true}})
$rbSearchOUWide.Text = "Search within OU"
$form.Controls.Add($rbSearchOUWide)


$OulableBox = new-object System.Windows.Forms.Label
$OulableBox.Location = new-object System.Drawing.Size(220,60)
$OulableBox.size = new-object System.Drawing.Size(120,20)
$OulableBox.Text = "Select OU Name : "
$form.controls.Add($OulableBox)

# Add OU Drop Down
$ouOUNameDrop = new-object System.Windows.Forms.ComboBox
$ouOUNameDrop.Location = new-object System.Drawing.Size(360,60)
$ouOUNameDrop.Size = new-object System.Drawing.Size(350,30)
$ouOUNameDrop.Enabled = $false
$root = [ADSI]'LDAP://RootDSE'
$dfDefaultRootPath = "LDAP://" + $root.DefaultNamingContext.tostring()
$dfRoot = [ADSI]$dfDefaultRootPath
$gfGALQueryFilter = "(objectClass=organizationalUnit)"
$dfsearcher = new-object System.DirectoryServices.DirectorySearcher($dfRoot)
$dfsearcher.Filter = $gfGALQueryFilter
$srSearchResult = $dfsearcher.FindAll()
foreach ($emResult in $srSearchResult) {
$OUobject = New-Object System.DirectoryServices.directoryentry
$OUobject = $emResult.GetDirectoryEntry()
$ouOUNameDrop.Items.Add($OUobject.distinguishedName.ToString())
}
$form.Controls.Add($ouOUNameDrop)

# Add Prop Drop Down
$exPropDrop = new-object System.Windows.Forms.ComboBox
$exPropDrop.Location = new-object System.Drawing.Size(240,580)
$exPropDrop.Size = new-object System.Drawing.Size(150,30)
$exPropDrop.Items.Add("extensionAttribute1")
$exPropDrop.Items.Add("extensionAttribute2")
$exPropDrop.Items.Add("extensionAttribute3")
$exPropDrop.Items.Add("extensionAttribute4")
$exPropDrop.Items.Add("extensionAttribute5")
$exPropDrop.Items.Add("extensionAttribute6")
$exPropDrop.Items.Add("extensionAttribute7")
$exPropDrop.Items.Add("extensionAttribute8")
$exPropDrop.Items.Add("extensionAttribute9")
$exPropDrop.Items.Add("extensionAttribute10")
$exPropDrop.Items.Add("extensionAttribute11")
$exPropDrop.Items.Add("extensionAttribute12")
$exPropDrop.Items.Add("extensionAttribute13")
$exPropDrop.Items.Add("extensionAttribute14")
$exPropDrop.Items.Add("extensionAttribute15")
$form.Controls.Add($exPropDrop)

$exlableBox = new-object System.Windows.Forms.Label
$exlableBox.Location = new-object System.Drawing.Size(20,580)
$exlableBox.size = new-object System.Drawing.Size(240,20)
$exlableBox.Text = "Extended Property to Set/Update : "
$form.controls.Add($exlableBox)

$exlableBox2 = new-object System.Windows.Forms.Label
$exlableBox2.Location = new-object System.Drawing.Size(20,610)
$exlableBox2.size = new-object System.Drawing.Size(150,20)
$exlableBox2.Text = "Value : "
$form.controls.Add($exlableBox2)


$uvtext = new-object System.Windows.Forms.TextBox
$uvtext.Location = new-object System.Drawing.Size(240,610)
$uvtext.size = new-object System.Drawing.Size(300,20)
$form.controls.Add($uvtext)


$ProxyAddresslableBox = new-object System.Windows.Forms.Label
$ProxyAddresslableBox.Location = new-object System.Drawing.Size(20,100)
$ProxyAddresslableBox.size = new-object System.Drawing.Size(320,20)
$ProxyAddresslableBox.Text = "Group to Search for"
$form.controls.Add($ProxyAddresslableBox)

$ouOUCheckBox = new-object System.Windows.Forms.CheckBox
$ouOUCheckBox.Location = new-object System.Drawing.Size(750,60)
$ouOUCheckBox.Size = new-object System.Drawing.Size(200,20)
$ouOUCheckBox.Checked = $true
$ouOUCheckBox.Text = "Search in Sub OU's"
$form.Controls.Add($ouOUCheckBox)


# Add ProxyDomain Text Box
$pnProxyAddress = new-object System.Windows.Forms.TextBox
$pnProxyAddress.Location = new-object System.Drawing.Size(350,100)
$pnProxyAddress.size = new-object System.Drawing.Size(300,20)
$form.controls.Add($pnProxyAddress)


$exButton1 = new-object System.Windows.Forms.Button
$exButton1.Location = new-object System.Drawing.Size(700,100)
$exButton1.Size = new-object System.Drawing.Size(125,20)
$exButton1.Text = "Search"
$exButton1.Add_Click({SearchfoGroup})
$form.Controls.Add($exButton1)

$exButton3 = new-object System.Windows.Forms.Button
$exButton3.Location = new-object System.Drawing.Size(320,150)
$exButton3.Size = new-object System.Drawing.Size(105,20)
$exButton3.Text = "Show Members"
$exButton3.Add_Click({$grTable.clear()
$repeathashUser.clear()
$repeathashGroup.clear()
Get-member($msTable.DefaultView[$dgDataGrid.CurrentCell.RowIndex][1])
})
$form.Controls.Add($exButton3)

# Add Update Button

$exButton2 = new-object System.Windows.Forms.Button
$exButton2.Location = new-object System.Drawing.Size(10,640)
$exButton2.Size = new-object System.Drawing.Size(125,20)
$exButton2.Text = "Set/Update Value"
$exButton2.Add_Click({
$repeathashUser.clear()
$repeathashGroup.clear()
update-member($msTable.DefaultView[$dgDataGrid.CurrentCell.RowIndex][1])
})
$form.Controls.Add($exButton2)


# Add Export Grid Button

$exButton4 = new-object System.Windows.Forms.Button
$exButton4.Location = new-object System.Drawing.Size(700,550)
$exButton4.Size = new-object System.Drawing.Size(125,20)
$exButton4.Text = "Export Grid"
$exButton4.Add_Click({ExportGrid})
$form.Controls.Add($exButton4)

# Add DataGrid View

$dgDataGrid = new-object System.windows.forms.DataGridView
$dgDataGrid.Location = new-object System.Drawing.Size(10,145)
$dgDataGrid.size = new-object System.Drawing.Size(300,400)
$dgDataGrid.AutoSizeRowsMode = "AllHeaders"
$form.Controls.Add($dgDataGrid)

# Add DataGrid View

$dgDataGrid2 = new-object System.windows.forms.DataGridView
$dgDataGrid2.Location = new-object System.Drawing.Size(440,145)
$dgDataGrid2.size = new-object System.Drawing.Size(400,400)
$dgDataGrid2.AutoSizeRowsMode = "AllHeaders"
$form.Controls.Add($dgDataGrid2)


$form.Text = "Group Extended Propery Update GUI"
$form.size = new-object System.Drawing.Size(1200,800)
$form.autoscroll = $true
$form.Add_Shown({$form.Activate()})
$form.ShowDialog()

Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.