1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
function Choose-ADOrganizationalUnit {
[CmdletBinding()]
param (
[string]$Title = "Choisir une unité d'organisation"
)
# Récupération de toutes les OUs de l'Active Directory
#$Ous = Get-ADOrganizationalUnit -Filter *
$Ous = @("toto", "tata", "titi")
# Création de la fenêtre
$Form = New-Object System.Windows.Forms.Form
$Form.Text = $Title
$Form.Size = New-Object System.Drawing.Size(500,200)
$Form.StartPosition = "CenterScreen"
# Création de la liste déroulante
$ComboBox = New-Object System.Windows.Forms.ComboBox
$ComboBox.Size = New-Object System.Drawing.Size(360,20)
$ComboBox.Location = New-Object System.Drawing.Point(50,50)
# Ajout des OUs à la liste déroulante
foreach ($Ou in $Ous) {
[void]$ComboBox.Items.Add($Ou)
}
$Form.Controls.Add($ComboBox)
# Création du bouton OK
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Text = "OK"
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Location = New-Object System.Drawing.Point(200,100)
$OKButton.Add_Click({
# Récupération de la OU sélectionnée
$Script:SelectedOu = $ComboBox.SelectedItem
# Fermeture de la fenêtre
$Form.Close()
})
$Form.Controls.Add($OKButton)
# Affichage de la fenêtre
$Form.ShowDialog() | Out-Null
# Retour de la OU sélectionnée
return $Script:SelectedOu
}
$result = Choose-ADOrganizationalUnit
write-host $result |
Partager