这似乎表明您是 GUI 设计的新手。这更好,但实际上你应该接受一些关于这个主题的培训。 Youtube is your friend.
您从表单中收集信息...
Import-Module ActiveDirectory
$ComputerName = 'SomeCurrentHostName'
$Location = 'NA/HQ/Building A'
$Description = 'I am a Windows 10 Computer'
Set-ADComputer -Identity $ComputerName -Location $Location -Description $Description
并将其传递给按钮的单击事件。
更谨慎的做法是创建一个函数进行测试,对其进行全面测试,然后将该函数分配给按钮单击事件。这样,您的 GUI 代码和代码隐藏是分开的,并且是隐藏的代码。
例如,对您展示的内容进行重构,如下所示。请注意,我只是将它作为演示输出到屏幕,显示从表单中捕获的值。真的没有理由输出到屏幕上。
###############################################################################
#region Begin initialize environment #
###############################################################################
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
###############################################################################
#endregion End initialize environment #
###############################################################################
###############################################################################
#region Begin Code library #
###############################################################################
Function Update-ComputerDetails
{
Write-Host $txtNewComputerName.Text
Write-Host $txtNewComputerLocation.Text
Write-Host $txtNewComputerDescription.Text
<#
Import-Module ActiveDirectory
$paramblock = @{
Identity = $txtNewComputerName.Text
Location = $txtNewComputerLocation.Text
Description = $txtNewComputerDescription.Text
}
Set-ADComputer @paramblock
#>
}
Function Exit-ComputerDetails
{
$frmComputerInfo.Close()
}
###############################################################################
#endregion End Code library #
###############################################################################
###############################################################################
#region Begin GUI code #
###############################################################################
$frmComputerInfo = New-Object system.Windows.Forms.Form
$frmComputerInfo.ClientSize = '489,157'
$frmComputerInfo.text = "Update Active Directory Computer Information"
$frmComputerInfo.TopMost = $false
$lblNewComputerName = New-Object system.Windows.Forms.Label
$lblNewComputerName.text = "New Computer Name"
$lblNewComputerName.AutoSize = $true
$lblNewComputerName.width = 25
$lblNewComputerName.height = 10
$lblNewComputerName.location = New-Object System.Drawing.Point(36,34)
$lblNewComputerName.Font = 'Microsoft Sans Serif,10'
$txtNewComputerName = New-Object system.Windows.Forms.TextBox
$txtNewComputerName.multiline = $false
$txtNewComputerName.width = 253
$txtNewComputerName.height = 20
$txtNewComputerName.location = New-Object System.Drawing.Point(189,29)
$txtNewComputerName.Font = 'Microsoft Sans Serif,10'
$lblNewComputerLocation = New-Object system.Windows.Forms.Label
$lblNewComputerLocation.text = "New Computer Location"
$lblNewComputerLocation.AutoSize = $true
$lblNewComputerLocation.width = 25
$lblNewComputerLocation.height = 10
$lblNewComputerLocation.location = New-Object System.Drawing.Point(36,58)
$lblNewComputerLocation.Font = 'Microsoft Sans Serif,10'
$txtNewComputerLocation = New-Object system.Windows.Forms.TextBox
$txtNewComputerLocation.multiline = $false
$txtNewComputerLocation.width = 252
$txtNewComputerLocation.height = 20
$txtNewComputerLocation.location = New-Object System.Drawing.Point(189,55)
$txtNewComputerLocation.Font = 'Microsoft Sans Serif,10'
$lblNewComputerDescription = New-Object system.Windows.Forms.Label
$lblNewComputerDescription.text = "New Description"
$lblNewComputerDescription.AutoSize = $true
$lblNewComputerDescription.width = 25
$lblNewComputerDescription.height = 10
$lblNewComputerDescription.location = New-Object System.Drawing.Point(36,85)
$lblNewComputerDescription.Font = 'Microsoft Sans Serif,10'
$txtNewComputerDescription = New-Object system.Windows.Forms.TextBox
$txtNewComputerDescription.multiline = $false
$txtNewComputerDescription.width = 252
$txtNewComputerDescription.height = 20
$txtNewComputerDescription.location = New-Object System.Drawing.Point(189,79)
$txtNewComputerDescription.Font = 'Microsoft Sans Serif,10'
$btnOK = New-Object system.Windows.Forms.Button
$btnOK.text = "OK"
$btnOK.width = 60
$btnOK.height = 30
$btnOK.location = New-Object System.Drawing.Point(306,107)
$btnOK.Font = 'Microsoft Sans Serif,10'
$btnCancel = New-Object system.Windows.Forms.Button
$btnCancel.text = "Cancel"
$btnCancel.width = 60
$btnCancel.height = 30
$btnCancel.location = New-Object System.Drawing.Point(379,107)
$btnCancel.Font = 'Microsoft Sans Serif,10'
$frmComputerInfo.controls.AddRange(@(
$lblNewComputerName,
$txtNewComputerName,
$lblNewComputerLocation,
$txtNewComputerLocation,
$lblNewComputerDescription,
$txtNewComputerDescription,
$btnOK,
$btnCancel
)
)
###############################################################################
#endregion End GUI code #
###############################################################################
###############################################################################
#region Begin logic execution code #
###############################################################################
$btnOK.Add_Click({ Update-ComputerDetails })
$btnCancel.Add_Click({ Exit-ComputerDetails })
[void]$frmComputerInfo.ShowDialog()
###############################################################################
#endregion End Logic execution code #
###############################################################################
或者这样更直接和简化:
# Custom Input Box
#region Begin environment initialization
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#endregion
#region Begin GUI code
$form = New-Object -TypeName System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object -TypeName System.Drawing.Size(300, 200)
$form.StartPosition = 'CenterScreen'
$label = New-Object -TypeName System.Windows.Forms.Label
$label.Location = New-Object -TypeName System.Drawing.Size(10, 20)
$label.Size = New-Object -TypeName System.Drawing.Size(280, 25)
$label.Text = 'Please enter the computername, location name, and description information in the spaces below'
$textBox1 = New-Object -TypeName System.Windows.Forms.TextBox
$textBox1.Location = New-Object -TypeName System.Drawing.Size(13, 50)
$textBox1.Size = New-Object -TypeName System.Drawing.Size(260, 20)
$textBox2 = New-Object -TypeName System.Windows.Forms.TextBox
$textBox2.Location = New-Object -TypeName System.Drawing.Size(13, 70)
$textBox2.Size = New-Object -TypeName System.Drawing.Size(260, 20)
$textBox3 = New-Object -TypeName System.Windows.Forms.TextBox
$textBox3.Location = New-Object -TypeName System.Drawing.Size(13, 90)
$textBox3.Size = New-Object -TypeName System.Drawing.Size(260, 20)
$OkButton = New-Object -TypeName System.Windows.Forms.Button
$OkButton.Location = New-Object -TypeName System.Drawing.Size(75, 120)
$OkButton.Size = New-Object -TypeName System.Drawing.Size(75, 23)
$OkButton.Text = 'OK'
$OkButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$CancelButton = New-Object -TypeName System.Windows.Forms.Button
$CancelButton.Location = New-Object -TypeName System.Drawing.Size(150, 120)
$CancelButton.Size = New-Object -TypeName System.Drawing.Size(75, 23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.Controls.AddRange(@(
$label,
$textBox1,
$textBox2,
$textBox3,
$OkButton,
$CancelButton
)
)
$form.add_Shown({$textBox.Select()})
#endregion
# Beging GUI presentation
$form.TopMost = $true
$form.AcceptButton = $OkButton
$form.CancelButton = $CancelButton
#endregion
#region Begin logic execution
$result = $form.ShowDialog()
If ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$textBox1.Text
$textBox2.Text
$textBox3.Text
}
Else{'Cancel was pressed. Application Exited.'}
#endregion
有几个预先构建的脚本可以满足您的需求,而无需 GUI。见这些:
Powershell script to set AD Computer description during OSD --- Download: SetComputerDesc.zip
Update Computer Description–PowerShell --- Download: Update_Comptuer_Description.txt
How to rename a domain computer with Windows PowerShell
更新
顺便说一句,我在评论中谈到的 OGV 是这样的:
(不,不是很漂亮 - 但可以使用)。
Start-Process -FilePath (
Get-ChildItem -Path 'd:\scripts' -Filter '*.ps1' |
Sort-Object -Property Name |
Out-GridView -Title 'Select a script to run' -OutputMode Single
).FullName -Wait
还有一些方法可以嵌套 OGV 响应以获得结果流。看到这个:
Creating a Simplistic GUI Interface with Out-GridView
和这个...
How To Use Out-Gridview As A GUI In PowerShell