首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单的InputBox函数

简单的InputBox函数
EN

Stack Overflow用户
提问于 2015-05-30 00:11:59
回答 3查看 118.8K关注 0票数 22

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

代码语言:javascript
复制
function popUp($text,$title) {
    $a = new-object -comobject wscript.shell
    $b = $a.popup($text,0,$title,0)
}

popUp "Enter your demographics" "Demographics"

但我找不到一个与获取请求输入的弹出窗口等效的工具。

当然,有Read-Line,但它是从控制台提示的。

然后是这个复杂的函数,对于要求输入一次或两次的脚本来说,这似乎有点过头了:

代码语言:javascript
复制
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"
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-05-30 00:50:23

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

代码语言:javascript
复制
[void][Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

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

$text = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)
票数 49
EN

Stack Overflow用户

发布于 2018-07-21 04:45:44

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

代码语言:javascript
复制
$us = Read-Host 'Enter Your User Name:' -AsSecureString
$pw = Read-Host 'Enter Your Password:' -AsSecureString

如果你像我上面的例子一样收集登录信息,这一点特别有用。如果您更喜欢将变量作为SecureString对象进行模糊处理,则可以像这样动态转换变量:

代码语言:javascript
复制
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($us))
[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))

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

代码语言:javascript
复制
$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

票数 5
EN

Stack Overflow用户

发布于 2015-05-30 00:20:21

应该是这样的

代码语言:javascript
复制
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
}

然后,您可以调用类似下面的函数。

代码语言:javascript
复制
$userInput = CustomInputBox "User Name" "Please enter your name." ""
if ( $userInput -ne $null ) 
{
 echo "Input was [$userInput]"
}
else
{
 echo "User cancelled the form!"
}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30534273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档