converter_adusers_attributes.ps1
<#PSScriptInfo .VERSION 1.0 .GUID 78e03b35-3ab6-44d5-9246-779479ce5d7a .AUTHOR achernobaev@hotmail.com .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES .PRIVATEDATA #> <# .DESCRIPTION Scenario for identifying users attributes in Microsoft Active Directory #> Param() #---------------------------------------------- # Generated Form Function #---------------------------------------------- function Show-Converter_ADUsers_attributes_psf { #---------------------------------------------- #region Import the Assemblies #---------------------------------------------- [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089') [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') #endregion Import Assemblies #---------------------------------------------- #region Generated Form Objects #---------------------------------------------- [System.Windows.Forms.Application]::EnableVisualStyles() $form_Converter_ADUsers_attributes = New-Object 'System.Windows.Forms.Form' $buttonClear = New-Object 'System.Windows.Forms.Button' $buttonConvert = New-Object 'System.Windows.Forms.Button' $richtextbox_attributes1 = New-Object 'System.Windows.Forms.RichTextBox' $richtextbox_attributes2 = New-Object 'System.Windows.Forms.RichTextBox' $checkbox_OnlyEnabledObjects = New-Object 'System.Windows.Forms.CheckBox' $button_Convert = New-Object 'System.Windows.Forms.Button' $combobox_attribute2 = New-Object 'System.Windows.Forms.ComboBox' $combobox_attribute1 = New-Object 'System.Windows.Forms.ComboBox' $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState' #endregion Generated Form Objects #---------------------------------------------- # User Generated Script #---------------------------------------------- $form_Converter_ADUsers_attributes_Load = { Import-Module activedirectory } #region Control Helper Functions function Update-ListBox { param ( [Parameter(Mandatory = $true)] [ValidateNotNull()] [System.Windows.Forms.ListBox] $ListBox, [Parameter(Mandatory = $true)] [ValidateNotNull()] $Items, [Parameter(Mandatory = $false)] [string]$DisplayMember, [Parameter(Mandatory = $false)] [string]$ValueMember, [switch] $Append ) if (-not $Append) { $ListBox.Items.Clear() } if ($Items -is [System.Windows.Forms.ListBox+ObjectCollection] -or $Items -is [System.Collections.ICollection]) { $ListBox.Items.AddRange($Items) } elseif ($Items -is [System.Collections.IEnumerable]) { $ListBox.BeginUpdate() foreach ($obj in $Items) { $ListBox.Items.Add($obj) } $ListBox.EndUpdate() } else { $ListBox.Items.Add($Items) } $ListBox.DisplayMember = $DisplayMember $ListBox.ValueMember = $ValueMember } function Update-ComboBox { param ( [Parameter(Mandatory = $true)] [ValidateNotNull()] [System.Windows.Forms.ComboBox] $ComboBox, [Parameter(Mandatory = $true)] [ValidateNotNull()] $Items, [Parameter(Mandatory = $false)] [string]$DisplayMember, [Parameter(Mandatory = $false)] [string]$ValueMember, [switch] $Append ) if (-not $Append) { $ComboBox.Items.Clear() } if ($Items -is [Object[]]) { $ComboBox.Items.AddRange($Items) } elseif ($Items -is [System.Collections.IEnumerable]) { $ComboBox.BeginUpdate() foreach ($obj in $Items) { $ComboBox.Items.Add($obj) } $ComboBox.EndUpdate() } else { $ComboBox.Items.Add($Items) } $ComboBox.DisplayMember = $DisplayMember $ComboBox.ValueMember = $ValueMember } #endregion $button_Convert_Click = { $atr1 = $combobox_attribute1.Text $atr2 = $combobox_attribute2.Text $usrs1 = $richtextbox_attributes1.Lines if ($checkbox_OnlyEnabledObjects.Checked) { $usrs2 = foreach ($usr in $usrs1) { (Get-ADUser -filter { $atr1 -like $usr } -properties $atr2 | Where-Object Enabled -eq $true).$atr2 } } else { $usrs2 = foreach ($usr in $usrs1) { (Get-ADUser -filter { $atr1 -like $usr } -properties $atr2 ).$atr2 } } $richtextbox_attributes2.lines = $usrs2 } $buttonConvert_Click={ $atr1 = $combobox_attribute1.Text $atr2 = $combobox_attribute2.Text $usrs2 = $richtextbox_attributes2.Lines if ($checkbox_OnlyEnabledObjects.Checked) { $usrs1 = foreach ($usr in $usrs2) { (Get-ADUser -filter { $atr2 -like $usr } -properties $atr1 | Where-Object Enabled -eq $true).$atr1 } } else { $usrs1 = foreach ($usr in $usrs2) { (Get-ADUser -filter { $atr2 -like $usr } -properties $atr1).$atr1 } } $richtextbox_attributes1.lines = $usrs1 } $buttonClear_Click={ $richtextbox_attributes1.lines = "" $richtextbox_attributes2.lines = "" } # --End User Generated Script-- #---------------------------------------------- #region Generated Events #---------------------------------------------- $Form_StateCorrection_Load= { #Correct the initial state of the form to prevent the .Net maximized form issue $form_Converter_ADUsers_attributes.WindowState = $InitialFormWindowState } $Form_Cleanup_FormClosed= { #Remove all event handlers from the controls try { $buttonClear.remove_Click($buttonClear_Click) $buttonConvert.remove_Click($buttonConvert_Click) $button_Convert.remove_Click($button_Convert_Click) $form_Converter_ADUsers_attributes.remove_Load($form_Converter_ADUsers_attributes_Load) $form_Converter_ADUsers_attributes.remove_Load($Form_StateCorrection_Load) $form_Converter_ADUsers_attributes.remove_FormClosed($Form_Cleanup_FormClosed) } catch { Out-Null <# Prevent PSScriptAnalyzer warning #> } } #endregion Generated Events #---------------------------------------------- #region Generated Form Code #---------------------------------------------- $form_Converter_ADUsers_attributes.SuspendLayout() # # form_Converter_ADUsers_attributes # $form_Converter_ADUsers_attributes.Controls.Add($buttonClear) $form_Converter_ADUsers_attributes.Controls.Add($buttonConvert) $form_Converter_ADUsers_attributes.Controls.Add($richtextbox_attributes1) $form_Converter_ADUsers_attributes.Controls.Add($richtextbox_attributes2) $form_Converter_ADUsers_attributes.Controls.Add($checkbox_OnlyEnabledObjects) $form_Converter_ADUsers_attributes.Controls.Add($button_Convert) $form_Converter_ADUsers_attributes.Controls.Add($combobox_attribute2) $form_Converter_ADUsers_attributes.Controls.Add($combobox_attribute1) $form_Converter_ADUsers_attributes.AutoScaleDimensions = '6, 13' $form_Converter_ADUsers_attributes.AutoScaleMode = 'Font' $form_Converter_ADUsers_attributes.ClientSize = '638, 494' $form_Converter_ADUsers_attributes.Name = 'form_Converter_ADUsers_attributes' $form_Converter_ADUsers_attributes.Text = 'Converter ADUsers attributes' $form_Converter_ADUsers_attributes.add_Load($form_Converter_ADUsers_attributes_Load) # # buttonClear # $buttonClear.Font = 'Microsoft Sans Serif, 9.75pt' $buttonClear.Location = '253, 302' $buttonClear.Name = 'buttonClear' $buttonClear.Size = '130, 23' $buttonClear.TabIndex = 9 $buttonClear.Text = 'clear' $buttonClear.UseCompatibleTextRendering = $True $buttonClear.UseVisualStyleBackColor = $True $buttonClear.add_Click($buttonClear_Click) # # buttonConvert # $buttonConvert.Font = 'Microsoft Sans Serif, 9.75pt' $buttonConvert.Location = '253, 262' $buttonConvert.Name = 'buttonConvert' $buttonConvert.Size = '130, 23' $buttonConvert.TabIndex = 8 $buttonConvert.Text = '< convert' $buttonConvert.UseCompatibleTextRendering = $True $buttonConvert.UseVisualStyleBackColor = $True $buttonConvert.add_Click($buttonConvert_Click) # # richtextbox_attributes1 # $richtextbox_attributes1.Location = '27, 71' $richtextbox_attributes1.Name = 'richtextbox_attributes1' $richtextbox_attributes1.Size = '220, 399' $richtextbox_attributes1.TabIndex = 7 $richtextbox_attributes1.Text = '' # # richtextbox_attributes2 # $richtextbox_attributes2.Location = '390, 71' $richtextbox_attributes2.Name = 'richtextbox_attributes2' $richtextbox_attributes2.Size = '220, 399' $richtextbox_attributes2.TabIndex = 6 $richtextbox_attributes2.Text = '' # # checkbox_OnlyEnabledObjects # $checkbox_OnlyEnabledObjects.Font = 'Microsoft Sans Serif, 9.75pt' $checkbox_OnlyEnabledObjects.Location = '268, 180' $checkbox_OnlyEnabledObjects.Name = 'checkbox_OnlyEnabledObjects' $checkbox_OnlyEnabledObjects.Size = '101, 34' $checkbox_OnlyEnabledObjects.TabIndex = 5 $checkbox_OnlyEnabledObjects.Text = 'only enabled objects' $checkbox_OnlyEnabledObjects.UseCompatibleTextRendering = $True $checkbox_OnlyEnabledObjects.UseVisualStyleBackColor = $True # # button_Convert # $button_Convert.Font = 'Microsoft Sans Serif, 9.75pt' $button_Convert.Location = '254, 222' $button_Convert.Name = 'button_Convert' $button_Convert.Size = '130, 23' $button_Convert.TabIndex = 4 $button_Convert.Text = 'convert >' $button_Convert.UseCompatibleTextRendering = $True $button_Convert.UseVisualStyleBackColor = $True $button_Convert.add_Click($button_Convert_Click) # # combobox_attribute2 # $combobox_attribute2.FormattingEnabled = $True [void]$combobox_attribute2.Items.Add('Name') [void]$combobox_attribute2.Items.Add('displayName') [void]$combobox_attribute2.Items.Add('mail') [void]$combobox_attribute2.Items.Add('SamAccountName') $combobox_attribute2.Location = '390, 24' $combobox_attribute2.Name = 'combobox_attribute2' $combobox_attribute2.Size = '220, 21' $combobox_attribute2.TabIndex = 3 # # combobox_attribute1 # $combobox_attribute1.FormattingEnabled = $True [void]$combobox_attribute1.Items.Add('Name') [void]$combobox_attribute1.Items.Add('displayName') [void]$combobox_attribute1.Items.Add('mail') [void]$combobox_attribute1.Items.Add('SamAccountName') $combobox_attribute1.Location = '27, 24' $combobox_attribute1.Name = 'combobox_attribute1' $combobox_attribute1.Size = '220, 21' $combobox_attribute1.TabIndex = 2 $form_Converter_ADUsers_attributes.ResumeLayout() #endregion Generated Form Code #---------------------------------------------- #Save the initial state of the form $InitialFormWindowState = $form_Converter_ADUsers_attributes.WindowState #Init the OnLoad event to correct the initial state of the form $form_Converter_ADUsers_attributes.add_Load($Form_StateCorrection_Load) #Clean up the control events $form_Converter_ADUsers_attributes.add_FormClosed($Form_Cleanup_FormClosed) #Show the Form return $form_Converter_ADUsers_attributes.ShowDialog() } #End Function #Call the form Show-Converter_ADUsers_attributes_psf | Out-Null |