it-wurzel
Goto Top

Prompt User für Active Directory (bzw. einer Domäne oder OU)?

Guten Morgen liebe Community,

meine heutige Frage,

"gibt es, wie z.B. unter vbs für Dateien und Ordner (Stichwort Window_Handle), unter Powershell die Möglichkeit (mit einer kleinen GUI z.B.) mir die Auswahl einer Domäne zu ermöglichen, um diese dann an das Skript zur weiteren Verarbeitung zu übergeben?"

(Falls ich mich zu kompliziert ausgedrückt haben sollte, bitte um Nachsicht und nachfrage face-wink )

Content-Key: 246914

Url: https://administrator.de/contentid/246914

Ausgedruckt am: 29.03.2024 um 08:03 Uhr

Mitglied: colinardo
colinardo 20.08.2014 aktualisiert um 10:21:51 Uhr
Goto Top
Moin,
"gibt es, wie z.B. unter vbs für Dateien und Ordner (Stichwort Window_Handle), unter Powershell die Möglichkeit (mit einer kleinen GUI z.B.) mir die Auswahl einer Domäne zu ermöglichen, um diese dann an das Skript zur weiteren Verarbeitung zu übergeben?"
yip, geht alles, da Powershell auf dem NET Framework aufbaut hast du hier das ganze Arsenal der Windows Forms etc. zur Verfügung:
back-to-topBeispiel: GUI mit Combobox
# Form Function
function GenerateForm {
#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
$btnOK = New-Object System.Windows.Forms.Button
$comboDomains = New-Object System.Windows.Forms.ComboBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#--------------------------------------
# Event Script Blocks
#--------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_btnOK_Click= 
{
    $script:resultDomain = $comboDomains.SelectedItem.ToString()
    $form1.Close()
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 47
$System_Drawing_Size.Width = 259
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.FormBorderStyle = 3
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.Name = "form1"  
$form1.StartPosition = 4
$form1.Text = "Domain auswählen"  


$btnOK.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 202
$System_Drawing_Point.Y = 12
$btnOK.Location = $System_Drawing_Point
$btnOK.Name = "btnOK"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 45
$btnOK.Size = $System_Drawing_Size
$btnOK.TabIndex = 1
$btnOK.Text = "OK"  
$btnOK.UseVisualStyleBackColor = $True
$btnOK.add_Click($handler_btnOK_Click)

$form1.Controls.Add($btnOK)

$comboDomains.DataBindings.DefaultDataSourceUpdateMode = 0
$comboDomains.DropDownStyle = 2
$comboDomains.FormattingEnabled = $True
$comboDomains.Items.Add("domain1.de")|Out-Null  
$comboDomains.Items.Add("domain2.de")|Out-Null  
$comboDomains.Items.Add("domain3.de")|Out-Null  
$comboDomains.SelectedIndex = 0
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$comboDomains.Location = $System_Drawing_Point
$comboDomains.Name = "comboDomains"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 184
$comboDomains.Size = $System_Drawing_Size
$comboDomains.TabIndex = 0

$form1.Controls.Add($comboDomains)

#endregion Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
return $script:resultDomain
} #End Function

#Call the Function
$domain = GenerateForm

# Anzeigen der Auswahl
write-host "Die ausgewählte Domain lautet: $domain"  

Alternativ wäre auch ein Textauswahlmenü in der Shell kein Problem. Stichwort Eingabeabfrage mit Read-Host

back-to-topBeispiel: Textauswahlmenü
$domains = @("Domain1.de","domain2.de","domain3.de")  
Write-host "Bitte wählen sie eine Domain:`r`n------------------------" -ForegroundColor Green  
$cnt = 1
$domains | %{write-host "`t[$cnt] $_" -ForegroundColor Yellow; $cnt++}  

$result = Read-Host "Ihre Auswahl"  
if ($domains[$result-1]){
    write-host "Sie haben folgende Domain gewählt: $($domains[$result-1])" -ForegroundColor Green  
}else{
    write-host "Sie haben keine gültige Auswahl getroffen!" -ForegroundColor Red  
}

Grüße Uwe
Mitglied: IT-Wurzel
IT-Wurzel 20.08.2014 um 10:36:12 Uhr
Goto Top
Hallo Uwe,

beide Lösungen sind grandios (wie immer!). Vielen Dank dazu.

aber als schmankerl'n, bzw. das i-Tüpfelchen ...

wie kann man die Abfrage so gestallten, dass er eine, oder die Domäne(n) automatisch ermittelt und sofern vorhanden, anzeigt, oder eben nur einen Alternativtext (z.B. "No Domain found", oder so ähnlich).

Grüße Mario
Mitglied: colinardo
colinardo 20.08.2014 aktualisiert um 10:41:13 Uhr
Goto Top
Zitat von @IT-Wurzel:
wie kann man die Abfrage so gestallten, dass er eine, oder die Domäne(n) automatisch ermittelt und sofern vorhanden, anzeigt,
Du willst alle Domains eines Forest auflisten ?
Mitglied: IT-Wurzel
IT-Wurzel 20.08.2014 um 10:52:34 Uhr
Goto Top
genau, das wäre das was perfekt wäre face-wink
Mitglied: colinardo
Lösung colinardo 20.08.2014 aktualisiert um 13:39:10 Uhr
Goto Top
dann check this:
#Form Function
function GenerateForm {
#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
$btnOK = New-Object System.Windows.Forms.Button
$comboDomains = New-Object System.Windows.Forms.ComboBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState
#endregion Generated Form Objects

#----------------------------------------------
#Generated Event Script Blocks
#----------------------------------------------
#Provide Custom Code for events specified in PrimalForms.
$handler_btnOK_Click= 
{
    $script:resultDomain = $comboDomains.SelectedItem.ToString()
    $form1.Close()
}
$handler_form1_Load=
{
    Try{
        $domains = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() | select -ExpandProperty Domains | select -ExpandProperty Name
        if ($domains.length -gt  0 ){
            $domains | %{$comboDomains.Items.Add($_)}
            $comboDomains.SelectedIndex = 0
        }else{
             [System.Windows.Forms.MessageBox]::Show("No Domains could be found in Forest!","Error",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error)  
            $script:resultDomain = "No Domain found"  
            $form1.Close()
        }
    }catch{
        [System.Windows.Forms.MessageBox]::Show("No Domains could be found!`r`nError: $($_.Exception.Message)","Error",[System.Windows.Forms.MessageBoxButtons]::OK,[System.Windows.Forms.MessageBoxIcon]::Error)  
        $script:resultDomain = "No Domain found"  
        $form1.Close()
    }
}

$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
}

#----------------------------------------------
#region Generated Form Code
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 47
$System_Drawing_Size.Width = 259
$form1.ClientSize = $System_Drawing_Size
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$form1.FormBorderStyle = 3
$form1.MaximizeBox = $False
$form1.MinimizeBox = $False
$form1.Name = "form1"  
$form1.StartPosition = 4
$form1.Text = "Domain auswählen"  
$form1.add_Load($handler_form1_Load)

$btnOK.DataBindings.DefaultDataSourceUpdateMode = 0

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 202
$System_Drawing_Point.Y = 12
$btnOK.Location = $System_Drawing_Point
$btnOK.Name = "btnOK"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 45
$btnOK.Size = $System_Drawing_Size
$btnOK.TabIndex = 1
$btnOK.Text = "OK"  
$btnOK.UseVisualStyleBackColor = $True
$btnOK.add_Click($handler_btnOK_Click)

$form1.Controls.Add($btnOK)

$comboDomains.DataBindings.DefaultDataSourceUpdateMode = 0
$comboDomains.DropDownStyle = 2
$comboDomains.FormattingEnabled = $True
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 12
$System_Drawing_Point.Y = 12
$comboDomains.Location = $System_Drawing_Point
$comboDomains.Name = "comboDomains"  
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Height = 21
$System_Drawing_Size.Width = 184
$comboDomains.Size = $System_Drawing_Size
$comboDomains.TabIndex = 0

$form1.Controls.Add($comboDomains)

#endregion Generated Form Code

#Save the initial state of the form
$InitialFormWindowState = $form1.WindowState
#Init the OnLoad event to correct the initial state of the form
$form1.add_Load($OnLoadForm_StateCorrection)
#Show the Form
$form1.ShowDialog()| Out-Null
return $script:resultDomain
} #End Function

#Call the Function
$domain = GenerateForm

# Anzeigen der Auswahl
write-host "Die ausgewählte Domain lautet: $domain"  
Mitglied: IT-Wurzel
IT-Wurzel 20.08.2014 um 12:23:32 Uhr
Goto Top
Hallo Uwe,

bei mehr als einer Dömane in einem Forrest funktioniert das Skript tadellos
Bei nur einer Domäne erscheint allerdings dann die Fehlermeldung "No Domains could be found ..."

Gruß,
Mario
Mitglied: colinardo
colinardo 20.08.2014 aktualisiert um 12:30:04 Uhr
Goto Top
Zitat von @IT-Wurzel:
bei mehr als einer Dömane in einem Forrest funktioniert das Skript tadellos
Bei nur einer Domäne erscheint allerdings dann die Fehlermeldung "No Domains could be found ..."
läuft hier bei mir auch mit einer einzigen Domäne ...

Bei nur einer Domäne erscheint allerdings dann die Fehlermeldung "No Domains could be found ..."
Diese Meldung erscheint wenn der Rechner in keiner Domäne ist, weil das Script dort in eine Exception läuft die ich abfange wenn der Rechner in keiner Domäne Mitglied ist. Der Rechner muss natürlich mit der Domäne verbunden sein, damit die Abfrage klappt.
Mitglied: IT-Wurzel
IT-Wurzel 20.08.2014 um 12:39:00 Uhr
Goto Top
Ok, dann hatte ich da einen Gedankenfehler ...

Ich hatte das Script in einer realen Umgebung auf einem Desktop getestet, und in einer Demo-Umgebung direkt auf dem Domänen Server ausgeführt.
(Auf einem Rechner in dieser Demo-Umgebung noch nicht.)

Ich gehe aber dann davon aus, dass das Script auch das macht was es soll.

Vielen Dank noch mal ....
Mitglied: colinardo
colinardo 20.08.2014 aktualisiert um 13:06:35 Uhr
Goto Top
Ok, dann hatte ich da einen Gedankenfehler ...
sorry, nee mein Fehler, wurde gefunden und ist oben korrigiert face-wink

Grüße Uwe