Création script
This commit is contained in:
parent
1b82503322
commit
90b005c54e
|
@ -0,0 +1,131 @@
|
|||
#Script créé par Nicolas Lang - Sous licence CC-BY-SA
|
||||
#https://nicolaslang.fr
|
||||
|
||||
|
||||
$nomanagerstring = "Sans Manager"
|
||||
$windowname = "Vue hierarchique de l'A.D."
|
||||
$all = get-aduser -filter 'enabled -eq $true' -Properties manager,department,directreports
|
||||
$arraylist = New-Object System.Collections.ArrayList
|
||||
$managegroups = ($all | select-object -Property samaccountname,name,manager | Group-Object -Property manager)
|
||||
$loop = $null
|
||||
#J'ajoute les données du treeview à l'aide d'une fonction. C'est pour cela qu'elle est déclarée avant le restant du script
|
||||
function Add-Treeview {
|
||||
$treeView1.Nodes.Add($nomanagerstring,$nomanagerstring) | Out-Null
|
||||
$userslevel = New-Object System.Collections.ArrayList
|
||||
foreach ($utilisateur in $all)
|
||||
{
|
||||
|
||||
$loop = $utilisateur.manager
|
||||
#"Manager de début pour $($utilisateur.samaccountname) : $loop"
|
||||
#$loop -eq $null
|
||||
$level = 0
|
||||
$array =@()
|
||||
While($loop -ne $null)
|
||||
{
|
||||
$level++
|
||||
$array += $loop
|
||||
$loop = ($all | Where-Object {$_.distinguishedname -eq $loop}).manager #récupère le manager de l'utilisateur / S / S+1 / S+x afin de déterminer l'endroit dans l'arborescence
|
||||
if ($array -contains $loop)
|
||||
{
|
||||
"$($utilisateur.samaccountname) boucle sur $loop"
|
||||
$array.Clear()
|
||||
break
|
||||
}
|
||||
}
|
||||
if (($level -eq 0) -and ($managegroups.name.contains($utilisateur.distinguishedname)) -ne $false)
|
||||
{
|
||||
$userslevel.Add([PSCUSTOMOBJECT]@{"name" = $utilisateur.name;"samaccountname" = $utilisateur.samaccountname;"Manager" = "Direction";"level" = $level;"Department" = $utilisateur.department}) |Out-Null
|
||||
}
|
||||
elseif ($level -eq 0)
|
||||
{
|
||||
$userslevel.Add([PSCUSTOMOBJECT]@{"name" = $utilisateur.name;"samaccountname" = $utilisateur.samaccountname;"Manager" = $nomanagerstring;"level" = $level;"Department" = $utilisateur.department}) | Out-Null
|
||||
}
|
||||
else
|
||||
{
|
||||
$userslevel.Add([PSCUSTOMOBJECT]@{"name" = $utilisateur.name;"samaccountname" = $utilisateur.samaccountname;"Manager" = $utilisateur.Manager;"level" = $level;"Department" = $utilisateur.department}) | Out-Null
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
$userslevel = $userslevel | Sort-Object -Property level
|
||||
|
||||
$groupedlevel = $userslevel | Group-Object -Property level
|
||||
foreach ($level in $groupedlevel){
|
||||
|
||||
foreach ($utilisateur in $level.Group)
|
||||
{
|
||||
|
||||
$ispresent = $null
|
||||
if ($utilisateur.Manager -eq $nomanagerstring)
|
||||
{
|
||||
$treeView1.nodes[$nomanagerstring].nodes.add($utilisateur.samaccountname, $utilisateur.name) | Out-Null
|
||||
}
|
||||
else
|
||||
{
|
||||
$managername = $all | Where-Object {$_.distinguishedname -eq $utilisateur.Manager}
|
||||
$ispresent = $treeView1.Nodes.Find($managername.samaccountname,$true)
|
||||
#@($utilisateur.name,$ispresent.count) | ft
|
||||
if ($($ispresent.count) -eq 0 -or ($ispresent -eq $null))
|
||||
{
|
||||
$treeView1.Nodes.Add($utilisateur.samaccountname,"$($utilisateur.name) - $($utilisateur.department)") | Out-Null
|
||||
}
|
||||
else
|
||||
{
|
||||
$ispresent[0].nodes.add($utilisateur.samaccountname,"$($utilisateur.name) - $($utilisateur.department)") | Out-Null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#Trier le treeview c'est mieux :)
|
||||
$treeView1.Sorted = $true
|
||||
}
|
||||
|
||||
|
||||
#################################################################
|
||||
|
||||
#region Import the Assemblies
|
||||
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
|
||||
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null
|
||||
#endregion
|
||||
|
||||
#region Generated Form Objects
|
||||
$form1 = New-Object System.Windows.Forms.Form
|
||||
$treeView1 = New-Object System.Windows.Forms.TreeView
|
||||
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
|
||||
#endregion Generated Form Objects
|
||||
|
||||
#region Generated Form Code
|
||||
$System_Drawing_Size = New-Object System.Drawing.Size
|
||||
$System_Drawing_Size.Height = 407
|
||||
$System_Drawing_Size.Width = 558
|
||||
$form1.ClientSize = $System_Drawing_Size
|
||||
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
|
||||
$form1.Name = "form1"
|
||||
$form1.Text = $windowname
|
||||
|
||||
|
||||
$treeView1.DataBindings.DefaultDataSourceUpdateMode = 0
|
||||
$System_Drawing_Point = New-Object System.Drawing.Point
|
||||
$System_Drawing_Point.X = 24
|
||||
$System_Drawing_Point.Y = 127
|
||||
$treeView1.Location = $System_Drawing_Point
|
||||
$treeView1.Name = "treeView1"
|
||||
$System_Drawing_Size = New-Object System.Drawing.Size
|
||||
$System_Drawing_Size.Height = 268
|
||||
$System_Drawing_Size.Width = 522
|
||||
$treeView1.Size = $System_Drawing_Size
|
||||
$treeView1.TabIndex = 0
|
||||
|
||||
Add-Treeview
|
||||
|
||||
$form1.Controls.Add($treeView1)
|
||||
|
||||
|
||||
$InitialFormWindowState = $form1.WindowState
|
||||
|
||||
$form1.add_Load($OnLoadForm_StateCorrection)
|
||||
|
||||
|
||||
$form1.ShowDialog()| Out-Null
|
Loading…
Reference in New Issue
Block a user