Question GUI Affichage de données des users

Plus d'informations
il y a 7 ans 4 mois #28040 par Philippe

Oui effectivement le problème ce trouve dans la lecture de cette variable $workingUsers et les fonctions Get-CurrentUser et Do-RenderUserDetail.

pour être précis c'est les lignes suivante de Get-CurrentUser qui ne font le travail comme dans le script originale
l'utilisation de l'uid à la place du DistinguishedName ne devrait rien changer au fonctionnement !

[code:1] ForEach ($attr in $script:fieldsConfig.Keys) {
$currentUser.Add($attr, $script:workingUsers.$currentuid.$attr)
}
[/code:1]
je continue a cherché

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 7 ans 4 mois #28050 par Philippe
trouver !!!! B) B) B) B) :woohoo: :woohoo:

alors pas simple au debut mais avec un bon débuggeur tous va mieux !!

tu a trois erreurs :

dans Get-Users tu a les keys qui était pas au bon format !!! collection au lieu de string, il faut faire [0] pour avoir que la valeur
[code:1] $users.Add($result.properties.uid[0], $user)
[/code:1]idem pour les valeurs [code:1] $user.Add($script:attributesToFields[$attribute][0], $result.properties.$attribute[0])
[/code:1]j'avais vite compris pour les valeurs mais pas pour la clé ! :(


dans Get-CurrentUser tu renvoie pas la bonne valeur ! uid au lieu de tous l'utilisateur
[code:1]return $User[/code:1]


et enfin dans Do-RenderUserDetail tu récupère mal la valeur uid
[code:1]$uid = $currentUser.uid[/code:1]


voila dis moi si c'est bon ! :)<br><br>Message édité par: 6ratgus, à: 23/01/19 10:47

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 7 ans 4 mois #28053 par Hicham Madini
6ratgus,

tu es vraiment une personne que j'apprécie et je respecte beaucoup!

la derniere fois tu m'as aidé à merger les deux scripts LDAP!

Cette fois aussi ca fonctionne trés bien!

Merci infiniment pour ton aide!

Je n'ai pas encore terminé ce que je veux réaliser:
Il me reste le DN je veux l'afficher sous Account Details avant le uid, cn, sn et mail. J'arrive à afficher le adspath complet mais où pourrais-je integrer \&quot; -replace \&quot;LDAP://ldap.forumsys.com:389/\&quot;, \&quot;\&quot;\&quot; ?

Merci mille fois 6ratgus

Message édité par: hichammadd, à: 23/01/19 11:42<br><br>Message édité par: hichammadd, à: 23/01/19 13:06

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 7 ans 4 mois #28054 par Philippe
merci pour les compliments
mais cette fois tu m'a donner du travail
je peut pas le faire a chaque fois !
c'est bien parce que le script me sera utile un jour que je m'y suis investi aussi longtemps

et puis je comprenais pas pourquoi la hastable me rendait pas les infos

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 7 ans 4 mois #28055 par Hicham Madini
ok pas de soucis je vais essayer de me debrouiller et si je trouve la soulution je la possterais!

Merci encore une fois.

Bon voici le script complet qui fonctionne.
Le seul soucis c'est sous le champs DN il mentre le contenu de adspath, mais je cherche une solution pour enlever \&quot;LDAP://ldap.forumsys.com:389/\&quot;.

[code:1][void] [System.Reflection.Assembly]::LoadWithPartialName(\&quot;System.Windows.Forms\&quot;«»)
[void] [System.Reflection.Assembly]::LoadWithPartialName(\&quot;System.Drawing\&quot;«»)
####### ## Global Variables #######
# working hash table of users being operated against (lookup results are stored in this)
$script:workingUsers = @{}

# list of fields to be included in results table and in detail form for individual account
$script:fieldsTable = @(\&quot;uid\&quot;, \&quot;sn\&quot;, \&quot;mail\&quot;«»)
$script:fieldsDetail = @(\&quot;DN\&quot;, \&quot;uid\&quot;, \&quot;cn\&quot;, \&quot;sn\&quot;, \&quot;mail\&quot;«»)

# fieldsConfig is a hash of field names with an array of (numLines, scrollEnabled, textbox)
$script:fieldsConfig = [ordered]@{
\&quot;DN\&quot; = (1, $False, $Null);
\&quot;uid\&quot; = (1, $False, $Null);
\&quot;cn\&quot; = (1, $False, $Null);
\&quot;sn\&quot; = (1, $False, $Null);
\&quot;mail\&quot; = (1, $False, $Null)
}

# attribute mapping to field label, and boolean indicated inclusion in new user form
$script:attributesToFields = @{
\&quot;adspath\&quot; =(\&quot;DN\&quot;, $False);
\&quot;uid\&quot; = (\&quot;uid\&quot;, $False);
\&quot;cn\&quot; = (\&quot;cn\&quot;, $True);
\&quot;sn\&quot; = (\&quot;sn\&quot;, $True);
\&quot;mail\&quot; = (\&quot;mail\&quot;, $True);
}

$script:buttonWidth = 150
$script:buttonHeight = 23
$script:buttonBaseX = 620

#######
## Helper Functions
#######

function Get-Users ($query=\&quot;\&quot;«») {
If ($query.Trim() -eq \&quot;\&quot;«») {
return @{}
}
$query = \&quot;*\&quot; + $query + \&quot;*\&quot;
$LDAP_Query = New-Object System.DirectoryServices.DirectorySearcher($Root,\&quot;(&amp;(objectclass=*)(uid=$query))\&quot;«»)
$results= $LDAP_Query.findall()
$users = @{}
ForEach ($result in $results) {
$user = @{}
ForEach ($attribute in $script:attributesToFields.Keys) {
If ($user.Keys -notContains $script:attributesToFields[$attribute][0]) {
$user.Add($script:attributesToFields[$attribute][0], $result.properties.$attribute[0])
}
}
$users.Add($result.properties.uid[0], $user)
}
return $users

}


function Do-AccountLookup ($strQuery) {
$script:workingUsers = Get-Users $strQuery
$objListView.Items.Clear()
$script:workingUsers.Keys | ForEach-Object {
$current_user = $_
$objListviewItem = New-Object System.Windows.Forms.ListViewItem($script:workingUsers.get_Item($current_user).get_Item($script:fieldsTable[0]))
ForEach ($field in $script:fieldsTable[1..($script:fieldsTable.Length - 1)]) {
$objListviewItem.SubItems.Add([string]$script:workingUsers.get_Item($current_user).get_Item($field)) | Out-Null
}
$objListview.Items.Add($objListviewItem) | Out-Null
}
}

#######
## GUI Event Handlers
#######

# handlers for static buttons

[System.EventHandler] $hndlrDoAccountLookup = {
$objListview.Items.Clear()
$Domaine = \&quot;LDAP://ldap.forumsys.com:389/dc=example,dc=com\&quot;
$Root = New-Object -TypeName System.DirectoryServices.DirectoryEntry($Domaine,$null,$null,'FastBind')
$query = $txtQuery.Text
Do-AccountLookup($query)

}

######### GUI Helpers #######

function Get-CurrentUser () {
$currentUser = $Null
$currentIdx = $objListView.SelectedIndices[0]
if ($currentIdx -ne $Null) {
$currentUser = @{}
$currentuid = $objListView.SelectedItems[0].Text
$currentUser.Add(\&quot;Index\&quot;, $currentIdx)
ForEach ($attr in $script:fieldsConfig.Keys) {
$currentUser.Add($attr, $script:workingUsers.$currentuid.$attr)
}
}
return $currentUser
}

Function Do-RenderUserDetail($currentUser=$Null) {

if ($currentUser -eq $Null) {
$currentUser = Get-CurrentUser
}

if ($currentUser -ne $Null) {
$uid = $currentUser.uid

foreach ($key in $script:fieldsDetail) {
$script:fieldsConfig.Get_Item($key)[2].Text = $script:workingUsers.get_Item($uid).get_Item($key)
$script:fieldsConfig.Get_Item($key)[2].ReadOnly = $True

}

}
else {
foreach ($key in $script:fieldsDetail) {
$script:fieldsConfig.Get_Item($key)[2].Text = \&quot;\&quot;
}
}

}


function Sort-ListView {
# from etechgoodness.wordpress.com/2014/02/25/s...t-a-custom-comparer/
param([parameter(Position=0)][UInt32]$Column)
$Numeric = $true # determine how to sort
if($Script:LastColumnClicked -eq $Column){
$Script:LastColumnAscending = -not $Script:LastColumnAscending
}
else {
$Script:LastColumnAscending = $true
}
$Script:LastColumnClicked = $Column
$ListItems = @(@(@())) # three-dimensional array; column 1 indexes the other columns,column 2 is the value to be
# sorted on, and column 3 is the System.Windows.Forms.ListViewItem object

foreach($ListItem in $objListView.Items) {
# if all items are numeric, can use a numeric sort
if($Numeric -ne $false) {
try {
$Test = [Double]$ListItem.SubItems.Text[$Column]
} catch {
$Numeric = $false # a non-numeric item was found, so sort will occur as a string
}
}
$ListItems += ,@($ListItem.SubItems.Text[$Column], $ListItem)
}

$EvalExpression = {
if($Numeric)
{ return [Double]$_[0] }
else
{ return [String]$_[0] }
}

$ListItems = $ListItems | Sort-Object -Property @{Expression=$EvalExpression; Ascending=$Script:LastColumnAscending}

$objListView.BeginUpdate()
$objListView.Items.Clear()
foreach($ListItem in $ListItems) {
$objListView.Items.Add($ListItem[1])
}
$objListView.EndUpdate()
}

#######
## GUI Initialization
#######

$frmUI = New-Object System.Windows.Forms.Form
$frmUI.Text = \&quot;User Account Managment\&quot;
$frmUI.Size = New-Object System.Drawing.Size(800,600)
$frmUI.StartPosition = \&quot;CenterScreen\&quot;
$frmUI.KeyPreview = $True
$frmUI.MaximumSize = $frmUI.Size
$frmUI.MinimumSize = $frmUI.Size

# first row - lookup input / button
$lblQuery = New-Object System.Windows.Forms.Label
$lblQuery.Location = New-Object System.Drawing.Size(5,5)
$lblQuery.Size = New-Object System.Drawing.Size(140,20)
$lblQuery.Text = \&quot;Username / Name:\&quot;
$frmUI.Controls.Add($lblQuery)

$txtQuery = New-Object System.Windows.Forms.TextBox
$txtQuery.Location = New-Object System.Drawing.Size(145,5)
$txtQuery.Size = New-Object System.Drawing.Size(270,20)
$frmUI.Controls.Add($txtQuery)

$btnLookup = New-Object System.Windows.Forms.Button
$btnLookup.Location = New-Object System.Drawing.Size(($buttonBaseX - $buttonWidth - 40),5)
$btnLookup.Size = New-Object System.Drawing.Size($buttonWidth,$buttonHeight)
$btnLookup.Text = \&quot;Lookup\&quot;
$btnLookup.Add_Click($hndlrDoAccountLookup)
$frmUI.Controls.Add($btnLookup)

# second row - lookup results
$lblResult = New-Object System.Windows.Forms.Label
$lblResult.Location = New-Object System.Drawing.Size(5,45)
$lblResult.Size = New-Object System.Drawing.Size(400,15)
$lblResult.Text = \&quot;Lookup Results\&quot;
$frmUI.Controls.Add($lblResult)

$objListview = New-Object System.Windows.Forms.Listview
$objListview.Location = New-Object System.Drawing.Size(5,60)
$objListview.Size = New-Object System.Drawing.Size(780,140)
$objListview.Height = 140
$objListview.View = [System.Windows.Forms.View]::«»Details
$objListView.MultiSelect = $False
$objListView.FullRowSelect = $True
foreach ($field in $script:fieldsTable) {
$col = $objListview.Columns.Add($field)
$col.Width = 120
}
$objListView.add_ColumnClick({Sort-ListView $_.Column})
$frmUI.Controls.Add($objListview)
$objListview.Add_SelectedIndexChanged({Do-RenderUserDetail})

# third row - individual account details
$lblAccount = New-Object System.Windows.Forms.Label
$lblAccount.Location = New-Object System.Drawing.Size(5,210)
$lblAccount.Size = New-Object System.Drawing.Size(400,15)
$lblAccount.Text = \&quot;Account Details\&quot;
$frmUI.Controls.Add($lblAccount)

$details_baseheight = 230
$details_rowheight = 25
$details_rowwidth = 480

# build out the text fields to hold the account details
$i = 0
ForEach ($fieldName in $script:fieldsDetail) {

$lbl = New-Object System.Windows.Forms.Label
$lbl.Location = New-Object System.Drawing.Size(5, ($details_baseheight + ($details_rowheight*$i)))
$lbl.Size = New-Object System.Drawing.Size(100,$details_rowheight)
$lbl.Text = $fieldName + \&quot;:\&quot;
$frmUI.Controls.Add($lbl)
$txt = New-Object System.Windows.Forms.TextBox
$txt.Location = New-Object System.Drawing.Size(105, ($details_baseheight + ($details_rowheight*$i)))
$rows = $script:fieldsConfig.Get_Item($fieldName)[0]
$scrollable = $script:fieldsConfig.Get_Item($fieldName)[1]
$txt.Size = New-Object System.Drawing.Size($details_rowwidth, (15*$rows))
$txt.ReadOnly = $True
if ($rows -gt 1) {
$txt.MultiLine = $True
}
if ($scrollable) {
$txt.ScrollBars = \&quot;Vertical\&quot;
}
$script:fieldsConfig.Get_Item($fieldName)[2] = $txt
$frmUI.Controls.Add($txt)
$i += 1
}


# misc gui stuff

$frmUI.Add_KeyDown({if ($_.KeyCode -eq \&quot;Enter\&quot;«») {$hndlrDoAccountLookup.Invoke($Null, [EventArgs]::Empty)}})
$frmUI.Add_KeyDown({if ($_.KeyCode -eq \&quot;Escape\&quot;«») {$frmUI.Close()}})

$frmUI.Add_Shown({$frmUI.Activate()})
[void] $frmUI.ShowDialog()[/code:1]


On peut fermer ce sujet alors.

à bientôt<br><br>Message édité par: hichammadd, à: 23/01/19 15:20

Connexion ou Créer un compte pour participer à la conversation.

Plus d'informations
il y a 7 ans 4 mois #28057 par Philippe
si tu ne veux que une modification a l'affichage, tu modifie Do-RenderUserDetail en changent cette boucle :
[code:1] foreach ($key in $script:fieldsDetail) {
if ($key -eq \&quot;DN\&quot;«») {
$script:fieldsConfig.Get_Item($key)[2].Text = $script:workingUsers.get_Item($uid).get_Item($key) -replace \&quot;LDAP://ldap.forumsys.com:389/\&quot;, \&quot;\&quot;
}
else {
$script:fieldsConfig.Get_Item($key)[2].Text = $script:workingUsers.get_Item($uid).get_Item($key)
}
$script:fieldsConfig.Get_Item($key)[2].ReadOnly = $True
}
[/code:1]

si par contre tu doit le modifier pour d'autre usage dans le script, ilfaudra le modifier a la source Do-AccountLookup

Connexion ou Créer un compte pour participer à la conversation.

Temps de génération de la page : 0.053 secondes
Propulsé par Kunena