前面我们介绍了怎么创建一个UI输入框的重置密码脚本,为了巩固我们再介绍一个通过UI输入框新建邮件通讯组的脚本。
UI界面:
#下面还是先编写一个输入框,但是这次为三个输入框我们需要注意一下协调各个Form和Label等的比例和位置参数
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "NEw-MailGroup"
$objForm.Size = New-Object System.Drawing.Size(300,230)
$objForm.StartPosition = "CenterScreen"
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$groupname=$objTextBox.Text;$objForm.Close()}}) #设置通讯组变量
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$Explain=$objTextBox1.Text;$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter")
{$GroupAdmin=$objTextBox2.Text;$objForm.Close()}}) # 设置通讯组管理员变量
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape")
{$objForm.Close()}})
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(55,150)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.Add_Click({$groupname=$objTextBox.Text;$objForm.Close()})
$OKButton.Add_Click({$Explain=$objTextBox1.Text;$objForm.Close()})
$OKButton.Add_Click({$GroupAdmin=$objTextBox2.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(170,150)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)
$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20)
$objLabel.Size = New-Object System.Drawing.Size(280,20)
$objLabel.Text = "Please Enter GroupAccount:"
$objForm.Controls.Add($objLabel)
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,40)
$objTextBox.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox)
$objLabel1 = New-Object System.Windows.Forms.Label
$objLabel1.Location = New-Object System.Drawing.Size(10,60)
$objLabel1.Size = New-Object System.Drawing.Size(280,20)
$objLabel1.Text = "The Group Explain:"
$objForm.Controls.Add($objLabel1)
$objTextBox1 = New-Object System.Windows.Forms.TextBox
$objTextBox1.Location = New-Object System.Drawing.Size(10,80)
$objTextBox1.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox1)
$objLabel2 = New-Object System.Windows.Forms.Label
$objLabel2.Location = New-Object System.Drawing.Size(10,100)
$objLabel2.Size = New-Object System.Drawing.Size(280,20)
$objLabel2.Text = "The Group's Manager:"
$objForm.Controls.Add($objLabel2)
$objTextBox2 = New-Object System.Windows.Forms.TextBox
$objTextBox2.Location = New-Object System.Drawing.Size(10,120)
$objTextBox2.Size = New-Object System.Drawing.Size(260,20)
$objForm.Controls.Add($objTextBox2)
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
New-DistributionGroup -Name "$groupname" -OrganizationalUnit "contoso.com/Beijing/allgroups" -SamAccountName "$groupname"# 创建一个通讯组
Set-DistributionGroup "$groupname" -ManagedBy $GroupAdmin #设置通讯组的管理员为XXX