从文本框类中获取值

时间:2016-01-27 21:36:06

标签: forms powershell textbox

我们有一个脚本是由不再和我们在一起的人写的。我是powershell的新用户,此函数未返回值:

# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$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({$x=$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,40) 
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel) 

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

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
}

我这样称呼它:

$ip = get-ip

我输入了一个值,我在$ip中没有得到任何内容。

如何捕获文本框的值?

FOR MARTIN

我做了Martin建议的更改,这是我的新代码:

# prompt user for ip/dns address input.
Function get-ip
{
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 

# Creates a message box that accepts dns/ip address input.
$objForm = New-Object System.Windows.Forms.Form
$objForm.Text = "User Input Required"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$script: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:x=$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,40) 
$objLabel.Text = "Please enter the IP address of the server you want to connect to:"
$objForm.Controls.Add($objLabel) 

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

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}

我收到与以前相同的错误消息:

PS E:\Dropbox\Powershell Scripts\SSS Cloud Icons> .\cloudicons.ps1
Creating Directories...
mkdir : Cannot bind argument to parameter 'Path' because it is an empty string.
At E:\Dropbox\Powershell Scripts\SSS Cloud Icons\cloudicons.ps1:80 char:4
+ md $_.userName
+    ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [mkdir], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,mkdir

我错过了什么吗?

**更新**

有人问我是怎么称呼get-ip:

# Insert user entered IP into pipeline.
$ip = get-ip
# Sets user entered IP into new file called propanehasip.rdp.
(get-content .\propanetest.rdp) -replace 'full address:s:INSERTIPHERE',"full address:s:$ip" | Out-File propanehasip.rdp

Write-Host "Creating Directories..."
Import-Csv $csv | ForEach-Object { 
# Creates directories based on the userName field.
md $_.userName
# Creates icons based on propanehasip.rdp and WSID fields.
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid1)" | out-file ".\$($_.username)\Propane $($_.wsid1).rdp"
(get-content ".\propanehasip.rdp") -replace 'remoteapplicationcmdline:s:INSERTWSIDHERE',"remoteapplicationcmdline:s:$($_.wsid2)" | out-file ".\$($_.username)\Propane $($_.wsid2).rdp"
}
}

更新#2

我想出了如何在visual studio中调试它。错误发生在这一行:

(get-content。\ propanetest.rdp)-replace'完整地址:s:INSERTIPHERE',"完整地址:s:$ ip" | Out-File propanehasip_new.rdp

1 个答案:

答案 0 :(得分:1)

$ x仅在块内部有效(变量$ x的范围),如果将其更改为$ script:x并在函数结束前添加$ x,则它可以正常工作。 需要额外的$ x来表示函数的返回值。

的变化:

$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {$script: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:x=$objTextBox.Text;$objForm.Close()})
$objForm.Controls.Add($OKButton)

功能结束:

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$x
}

并且...此脚本取自here,并应演示Windows.Forms的使用。但是,我没有找到发布日期。