【问题标题】:Having issues passing input from textbox从文本框传递输入时出现问题
【发布时间】:2017-09-05 22:51:31
【问题描述】:

我在从 PowerShell 中的文本框读取输入时遇到问题。代码如下:

$button6 = New-Object System.Windows.Forms.Button
$button6.Text = "Disable"
$button6.Width = 60
$button6.Height = 30
$button6.Add_Click({
   $script:id = $useridbox.Text;
   $script:db = $disabledby.Text;
   $script:date = Get-Date;
   Set-ADUser -Identity $id -Description "Disabled on $date by $db";
   Disable-ADAccount -Identity $id;
   Move-ADObject -Identity (Get-ADUser $id).ObjectGuid -TargetPath 'ou=Disabled and Terminated Accounts, dc=domain, dc=domain';
})

$button6.Location = New-Object System.Drawing.Point(136, 251)
$button6.Font = "Microsoft Sans Serif,10"
$Form.Controls.Add($button6)

因此,您在标题为 useridbox 的文本框中键入用户 ID,单击按钮,PowerShell 实际上并没有读取输入的值。我不断收到错误消息

Set-ADUser : 找不到具有标识的对象:''

我不确定我的范围

$script:id = $useridbox.Text; 

是正确的,或者如果我必须在脚本的前面声明变量,或者我在这里缺少什么导致了这种情况。

【问题讨论】:

  • $useridbox.Text 是否提供您认为的文本?您的代码太不完整,我们无法分辨。请将其设为minimal reproducible example
  • 我想我可能都用错了,
  • 当我声明变量 $Script:id = $useridbox.Text;我的想法是,当单击按钮时,它会抓取当时输入到文本框 $useridbox 中的任何文本。
  • 它应该获取单击按钮时文本框中存在的任何文本。

标签: winforms powershell


【解决方案1】:

好吧,我现在觉得自己像个白痴,但我发现了问题。

由于某种原因代码块

$useridbox = New-Object system.windows.Forms.TextBox
$useridbox.Width = 117
$useridbox.Height = 20
$useridbox.location = new-object system.drawing.point(136,18)
$useridbox.Font = "Microsoft Sans Serif,10"
$Form.controls.Add($useridbox)

在我的脚本中定义了标记为“useridbox”的文本框两次,我不知道为什么。我删除了多余的一个,我的脚本运行良好。

感谢所有试图回答的人。

【讨论】:

    猜你喜欢
    • 2011-09-26
    • 1970-01-01
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多