简单的InputBox功能

Rho*_*nda 16 powershell user-input function

我知道PowerShell的一个简单的弹出功能,例如:

function popUp($text,$title) {
    $a = new-object -comobject wscript.shell
    $b = $a.popup($text,0,$title,0)
}

popUp "Enter your demographics" "Demographics"
Run Code Online (Sandbox Code Playgroud)

但我无法找到一个等效的弹出窗口来请求输入.

当然,有Read-Line,但它从控制台提示.

然后有这个复杂的功能,对于要求输入一次或两次的脚本来说似乎有点过分:

function getValues($formTitle, $textTitle){
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = $formTitle
    $objForm.Size = New-Object System.Drawing.Size(300,200)
    $objForm.StartPosition = "CenterScreen"

    $objForm.KeyPreview = $True
    $objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$objTextBox.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(75,120)
    $OKButton.Size = New-Object System.Drawing.Size(75,23)
    $OKButton.Text = "OK"
    $OKButton.Add_Click({$Script:userInput=$objTextBox.Text;$objForm.Close()})
    $objForm.Controls.Add($OKButton)

    $CANCELButton = New-Object System.Windows.Forms.Button
    $CANCELButton.Location = New-Object System.Drawing.Size(150,120)
    $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,30)
    $objLabel.Text = $textTitle
    $objForm.Controls.Add($objLabel)

    $objTextBox = New-Object System.Windows.Forms.TextBox
    $objTextBox.Location = New-Object System.Drawing.Size(10,50)
    $objTextBox.Size = New-Object System.Drawing.Size(260,20)
    $objForm.Controls.Add($objTextBox)

    $objForm.Topmost = $True

    $objForm.Add_Shown({$objForm.Activate()})

    [void] $objForm.ShowDialog()

    return $userInput
}

$schema = getValues "Database Schema" "Enter database schema"
Run Code Online (Sandbox Code Playgroud)

Ans*_*ers 35

可能最简单的方法是使用类的InputBox方法Microsoft.VisualBasic.Interaction:

[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$title = 'Demographics'
$msg   = 'Enter your demographics:'

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
Run Code Online (Sandbox Code Playgroud)

  • 通过以现代方式加载程序集,它可以正常工作(即使在 ISE 中);只需将第一行替换为“Add-Type -AssemblyName Microsoft.VisualBasic” (6认同)

DBA*_*Don 5

获取输入框的最简单方法是使用 Read-Host cmdlet 和 -AsSecureString 参数。

$us = Read-Host 'Enter Your User Name:' -AsSecureString
$pw = Read-Host 'Enter Your Password:' -AsSecureString
Run Code Online (Sandbox Code Playgroud)

如果您像上面的示例一样收集登录信息,这将特别有用。如果您希望将变量混淆为 SecureString 对象,您可以像这样动态转换变量:

[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($us))
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))
Run Code Online (Sandbox Code Playgroud)

如果信息根本不需要安全,您可以将其转换为纯文本:

$user = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($us))
Run Code Online (Sandbox Code Playgroud)

Read-Host 和 -AsSecureString 似乎已包含在所有 PowerShell 版本 (1-6) 中,但我没有 PowerShell 1 或 2 以确保命令的工作方式相同。 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/read-host?view=powershell-3.0