如何使用Powershell GUI创建AD用户?

时间:2018-01-29 10:52:19

标签: powershell user-interface

我正在尝试创建一个可以创建新AD用户的GUI。 我的问题:当我按下按钮“Speichern”时,它不会使用文本框中的数据来创建AD用户。相反,此文本出现在命令控制台中:

New ADUser cmdlet at command pipeline location 1
Specify values ​​for the following parameters:
Surname:

如果我输入姓氏,它将使用此姓氏创建用户。 这是我的Powershell脚本:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

Import-Module ActiveDirectory

#GUI Oberfläche
$Form = New-Object System.Windows.Forms.Form
$Form.Size = New-Object System.Drawing.Size(500,500)
$Form.Text = "Benutzer hinzufügen"

#-------Labels-----------
#Label Benutzer hinzufügen
$labeladduser = New-Object System.Windows.Forms.Label
$labeladduser.Location = New-Object System.Drawing.Size(200,10) 
$labeladduser.Size = New-Object System.Drawing.Size(200,30) 
$labeladduser.Text = "Benutzer hinzufügen"
$labeladduser.Name = "Benutzer hinzufügen"
$Form.Controls.Add($labeladduser)

#Label Vorname
$Labelvorname = New-Object System.Windows.Forms.Label
$Labelvorname.Location = New-Object System.Drawing.Size(10,50) 
$Labelvorname.Size = New-Object System.Drawing.Size(100,20) 
$Labelvorname.Text = "Vorname"
$Labelvorname.Name = "Vorname"
$Form.Controls.Add($Labelvorname)

#Label Nachname
$Labelnachname = New-Object System.Windows.Forms.Label
$Labelnachname.Location = New-Object System.Drawing.Size(10,80) 
$Labelnachname.Size = New-Object System.Drawing.Size(100,20) 
$Labelnachname.Text = "Nachname"
$Labelnachname.Name = "Nachname"
$Form.Controls.Add($Labelnachname)

#Label Vollständigername
$LabelVn = New-Object System.Windows.Forms.Label
$LabelVn.Location = New-Object System.Drawing.Size(10,110) 
$LabelVn.Size = New-Object System.Drawing.Size(100,20) 
$LabelVn.Text = "Vollständigername"
$LabelVn.Name = "Vollständigername"
$Form.Controls.Add($LabelVn)

#Label Benutzeranmeldename
$LabelBa = New-Object System.Windows.Forms.Label
$LabelBa.Location = New-Object System.Drawing.Size(10,140) 
$LabelBa.Size = New-Object System.Drawing.Size(150,20) 
$LabelBa.Text = "Benutzeranmeldename"
$LabelBa.Name = "Benutzeranmeldename"
$Form.Controls.Add($LabelBa)

#-------Buttons-------
#Close GUI Button
$BTcancel = New-Object System.Windows.Forms.Button
$BTcancel.Location = New-Object System.Drawing.Size(400,180)
$BTcancel.Size = New-Object System.Drawing.Size(75,23)
$BTcancel.Text = "Cancel"
$BTcancel.Name = "Cancel"
$BTcancel.Add_Click({$Form.Close()})
$Form.Controls.Add($BTcancel) 

#Speichern Button
$BTsave = New-Object System.Windows.Forms.Button
$BTsave.Location = New-Object System.Drawing.Size(320,180)
$BTsave.Size = New-Object System.Drawing.Size(75,23)
$BTsave.Text = "Speichern"
$BTsave.Name = "Speichern"
$Form.Controls.Add($BTsave) 

#------Textfields------
#Textfeld Vorname
$Textboxvorname = New-Object System.Windows.Forms.TextBox 
$Textboxvorname.Location = New-Object System.Drawing.Size(200,50) 
$Textboxvorname.Size = New-Object System.Drawing.Size(200,20) 
$addv = $Textboxvorname.Text
$Form.Controls.Add($Textboxvorname)

#Textfeld Nachname
$Textboxnachname = New-Object System.Windows.Forms.TextBox 
$Textboxnachname.Location = New-Object System.Drawing.Size(200,80) 
$Textboxnachname.Size = New-Object System.Drawing.Size(200,20) 
$addn = $Textboxnachname.Text
$Form.Controls.Add($Textboxnachname)

#Textfeld Vollständigername
$TextboxVa = New-Object System.Windows.Forms.TextBox 
$TextboxVa.Location = New-Object System.Drawing.Size(200,110) 
$TextboxVa.Size = New-Object System.Drawing.Size(200,20) 
$addVa = $TextboxVa.Text
$Form.Controls.Add($TextboxVa)

#Textfeld Benutzeranmeldename
$TextboxBa= New-Object System.Windows.Forms.TextBox 
$TextboxBa.Location = New-Object System.Drawing.Size(200,140) 
$TextboxBa.Size = New-Object System.Drawing.Size(200,20) 
$addBa = $TextboxBa.Text
$Form.Controls.Add($TextboxBa)

#------Funktionen--------
#Add User
$BTsave.Add_Click({
New-ADuser
 -Name $addVa
 -Path "Ou=Users, DC=domain, DC=com"
 -Givenname = $addv
 -Surname =$addn
 -SamAccountName $addBa
 -AccountPassword (ConvertT0-SecureString "<redacted>" -AsPlainText -Force)
})

[void] $Form.ShowDialog()

enter image description here 有人为我的问题找到解决方案吗?

1 个答案:

答案 0 :(得分:2)

相信问题在于您分配值时。

以此为例:

#Textfeld Benutzeranmeldename
$TextboxBa= New-Object System.Windows.Forms.TextBox 
$TextboxBa.Location = New-Object System.Drawing.Size(200,140) 
$TextboxBa.Size = New-Object System.Drawing.Size(200,20) 
$addBa = $TextboxBa.Text
$Form.Controls.Add($TextboxBa)

当您将值分配给$addBa时,$TextboxBa.Text的值很可能为空!

所以你应该改变你的$BTsave.Add_Click事件来直接访问文本框值而不是空变量。

另外值得注意的是,PowerShell并不热衷于换行。如果你想避免长行,你必须添加一个反向字符。

#Add User
$BTsave.Add_Click({
    New-ADuser `
        -Name $TextboxVa.Text `
        -Path "Ou=Users, DC=domain, DC=com" `
        -Givenname $Textboxvorname.Text `
        -Surname $Textboxnachname.Text `
        -SamAccountName $TextboxBa.Text `
        -AccountPassword (ConvertTo-SecureString "<redacted>" -AsPlainText -Force)
})