您是否具有需要接受文本输入的PowerShell GUI?也许您希望能够复制并粘贴计算机,IP地址,用户名或其他数据的列表。我将向您展示如何在PowerShell GUI中添加文本框来处理该输入。如果您还没有创建PowerShell GUI,但是对此感兴趣,请查看我以前的文章中有关如何制作简单GUI的文章。
码
让我们开始分解实现此目标所需的代码部分。我喜欢将输入框代码放在函数中,但是您可能会决定不想这样做。要查看整个代码,请查看我的GitHub。
我们需要做的第一件事是通过.NET Windows窗体命名空间创建窗体。在这里,我们在窗口标题中指定所需的文本,以及所需的大小。某些可选设置是使窗口开始于屏幕中心,以防止调整窗口大小并将该窗口置于其他打开的窗口之上。
1个
2
3
4
5
6
7
8
9
|
### Creating the form with the Windows forms namespace
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form .Text = 'Enter the appropriate information' ### Text to be displayed in the title
$form .Size = New-Object System.Drawing.Size(310,625) ### Size of the window
$form .StartPosition = 'CenterScreen' ### Optional - specifies where the window should start
$form .FormBorderStyle = [System.Windows.Forms.FormBorderStyle] ::FixedToolWindow ### Optional - prevents resize of the window
$form .Topmost = $true ### Optional - Opens on top of other windows
|
接下来,我们将在窗口底部添加一个确定按钮。输入数据后,用户将单击“确定”按钮将输入传递到GUI的下一步。
1个
2
3
4
5
6
7
8
|
### Adding an OK button to the text box window
$OKButton = New-Object System.Windows.Forms.Button
$OKButton .Location = New-Object System.Drawing.Point(155,550) ### Location of where the button will be
$OKButton .Size = New-Object System.Drawing.Size(75,23) ### Size of the button
$OKButton .Text = 'OK' ### Text inside the button
$OKButton .DialogResult = [System.Windows.Forms.DialogResult] ::OK
$form .AcceptButton = $OKButton
$form .Controls.Add( $OKButton )
|
然后,如果用户决定不运行GUI,我们将添加一个取消按钮。
1个
2
3
4
5
6
7
8
|
### Adding a Cancel button to the text box window
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton .Location = New-Object System.Drawing.Point(70,550) ### Location of where the button will be
$CancelButton .Size = New-Object System.Drawing.Size(75,23) ### Size of the button
$CancelButton .Text = 'Cancel' ### Text inside the button
$CancelButton .DialogResult = [System.Windows.Forms.DialogResult] ::Cancel
$form .CancelButton = $CancelButton
$form .Controls.Add( $CancelButton )
|
为了避免混淆并帮助用户了解要输入什么类型的数据,我们将添加标签。该标签将由调用函数时使用的参数定义。如果要对标签文本进行硬编码,只需将$ Input_Type替换为要显示的文本。
1个
2
3
4
5
6
7
8
9
|
### Putting a label above the text box
$label = New-Object System.Windows.Forms.Label
$label .Location = New-Object System.Drawing.Point(10,10) ### Location of where the label will be
$label .AutoSize = $True
$Font = New-Object System.Drawing.Font( "Arial" ,12, [System.Drawing.FontStyle] ::Bold) ### Formatting text for the label
$label .Font = $Font
$label .Text = $Input_Type ### Text of label, defined by the parameter that was used when the function is called
$label .ForeColor = 'Red' ### Color of the label text
$form .Controls.Add( $label )
|
GUI窗口的最后一部分是文本框,它将用于接受输入的文本。
1个
2
3
4
5
6
7
8
|
### Inserting the text box that will accept input
$textBox = New-Object System.Windows.Forms.TextBox
$textBox .Location = New-Object System.Drawing.Point(10,40) ### Location of the text box
$textBox .Size = New-Object System.Drawing.Size(275,500) ### Size of the text box
$textBox .Multiline = $true ### Allows multiple lines of data
$textbox .AcceptsReturn = $true ### By hitting enter it creates a new line
$textBox .ScrollBars = "Vertical" ### Allows for a vertical scroll bar if the list of text is too big for the window
$form .Controls.Add( $textBox )
|
将画龙点睛放在一起,使它们一起工作并清理输入的数据。
1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
$form .Add_Shown({
$textBox .Select()}) ### Activates the form and sets the focus on it
$result = $form .ShowDialog() ### Displays the form
### If the OK button is selected do the following
if ( $result -eq [System.Windows.Forms.DialogResult] ::OK)
{
### Removing all the spaces and extra lines
$x = $textBox .Lines | Where{
$_ } | ForEach { $_ .Trim() }
### Putting the array together
$array = @()
### Putting each entry into array as individual objects
$array = $x -split "`r`n"
### Sending back the results while taking out empty objects
Return $array | Where-Object {
$_ -ne '' }
}
### If the cancel button is selected do the following
if ( $result -eq [System.Windows.Forms.DialogResult] ::Cancel)
{
Write-Host "User Canceled" -BackgroundColor Red -ForegroundColor White
Write-Host "Press any key to exit..."
$Host .UI.RawUI.ReadKey( "NoEcho,IncludeKeyDown" )
Exit
}
|
呼叫文字方块
既然创建GUI文本框的代码已经完成,我们需要调用文本框并返回输入的数据。我将分享一些有关如何利用我们刚创建的文本框的示例。下面的示例假定您已使用参数将文本框放入函数中。
1个
|
Function GUI_TextBox ( $Input_Type ){...}
|
这是一个如何使用文本框接受计算机名称列表的示例。本示例将仅输出输入到屏幕的计算机名称列表。为了使此功能有用,您将需要添加代码以使脚本对这些计算机名称起作用。也许您需要在计算机列表上执行电源操作或任何数量的管理员操作。
1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
###############################################################################
### Computer Name(s) example of how to utilize the GUI_TextBox function
###############################################################################
$Computers = GUI_TextBox "Computer Names(s):" ### Calls the text box function with a parameter and puts returned input in variable
$Computer_Count = $Computers | Measure-Object | % {
$_ .Count} ### Measures how many objects were inputted
If ( $Computer_Count -eq 0){ ### If the count returns 0 it will throw and error
Write-Host "Nothing was inputed..." -BackgroundColor Red -ForegroundColor White
Return
}
Else { ### If there was actual data returned in the input, the script will continue
Write-Host "Number of computers entered:" $Computer_Count -BackgroundColor Cyan -ForegroundColor Black
$Computers
### Here is where you would put your specific code to take action on those computers inputted
}
|
这是一个如何使用文本框接受用户列表的示例。本示例将仅输出输入到屏幕的用户列表。为了使此功能有用,您将需要添加代码以使脚本对该用户执行操作。也许您需要禁用其Active Directory帐户或任何数量的管理员操作。
1个
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
###############################################################################
### User Name(s) example of how to utilize the GUI_TextBox function
###############################################################################
$Users = GUI_TextBox "User Names(s):" ### Calls the text box function with a parameter and puts returned input in variable
$User_Count = $Users | Measure-Object | % {
$_ .Count} ### Measures how many objects were inputted
If ( $User_Count -eq 0){ ### If the count returns 0 it will throw and error
Write-Host "Nothing was inputed..." -BackgroundColor Red -ForegroundColor White
Return
}
Else { ### If there was actual data returned in the input, the script will continue
Write-Host "Number of users entered:" $User_Count -BackgroundColor Cyan -ForegroundColor Black
$Users
### Here is where you would put your specific code to take action on those users inputted
}
|
文字框在行动
完成后,您应该具有以下外观:
就是这样,感谢您的阅读!