【问题标题】:Simple InputBox function简单的输入框函数
【发布时间】:2015-05-29 16:11:59
【问题描述】:

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

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

popUp "Enter your demographics" "Demographics"

但我无法找到一个等效的弹出窗口来询问输入。

当然,有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"

【问题讨论】:

    标签: function powershell user-input


    【解决方案1】:

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

    [void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
    
    $title = 'Demographics'
    $msg   = 'Enter your demographics:'
    
    $text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
    

    【讨论】:

    • 我不同意上述方法是一种好方法 - 将 VB 与 ISE 结合使用尤其会导致挂起。
    • 通过以现代方式加载程序集,它可以正常工作(即使在 ISE 中);只需将第一行替换为Add-Type -AssemblyName Microsoft.VisualBasic
    【解决方案2】:

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

    $us = Read-Host 'Enter Your User Name:' -AsSecureString
    $pw = Read-Host 'Enter Your Password:' -AsSecureString
    

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

    [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($us))
    [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))
    

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

    $user = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($us))
    

    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

    【讨论】:

    • 很有趣,但是您看不到自己的输入,这使得 UX 很难。
    【解决方案3】:

    应该是这样的

    function CustomInputBox([string] $title, [string] $message, [string] $defaultText) 
    {
    $inputObject = new-object -comobject MSScriptControl.ScriptControl
    $inputObject.language = "vbscript" 
    $inputObject.addcode("function getInput() getInput = inputbox(`"$message`",`"$title`" , `"$defaultText`") end function" ) 
    $_userInput = $inputObject.eval("getInput") 
    
    return $_userInput
    }
    

    然后就可以调用类似这样的函数了。

    $userInput = CustomInputBox "User Name" "Please enter your name." ""
    if ( $userInput -ne $null ) 
    {
     echo "Input was [$userInput]"
    }
    else
    {
     echo "User cancelled the form!"
    }
    

    这是我能想到的最简单的方法。

    【讨论】:

    • 在 Windows 10 Pro 上无法开箱即用:new-object : Retrieving the COM class factory for component with CLSID {0E59F1D5-1FBE-11D0-8FF2-00A0D10038BC} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多