0

I'm having an issues reading input from a textbox in PowerShell. Code below:

$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)

So you type the userid in the textbox titled useridbox, click the button and PowerShell is not actually reading the value entered. I keep getting the error message

Set-ADUser : Cannot find an object with identity: ''

I'm not sure if my scope

$script:id = $useridbox.Text; 

is correct or if I have to declare the variable earlier in the script or what exactly I'm missing here that is causing this.

4
  • Does $useridbox.Text give the text you think it does? Your code is too incomplete for us to tell. Please make it a minimal reproducible example. Commented Apr 10, 2017 at 19:24
  • I think I might be using this wrong all together, Commented Apr 10, 2017 at 19:27
  • When I declared the variable $Script:id = $useridbox.Text; My thinking was that as the button was clicked it would grab whatever text was entered into the textbox $useridbox at the time. Commented Apr 10, 2017 at 19:28
  • It should get whatever text is present in the textbox at the time the button is clicked. Commented Apr 10, 2017 at 19:30

1 Answer 1

1

Well I feel like an idiot now but I found the problem.

For some reason the code block

$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)

Defining the textbox labelled "useridbox" was in my script two times and I have no idea why. I deleted the extra one and my script worked perfectly.

Thank you everybody that attempted to answer.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.