readyrockc
readyrockc

Reputation: 1

Powershell GUI script: read variable from textbox

I'm trying to pass the input of the two textboxes to the logic part of the script at the bottom. But that's not happening and I can't figure out the problem... I tried rewriting the code from [here][1].

Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form
$form.Text = 'CompareGroups'
$form.Size = New-Object System.Drawing.Size(300,250)
$form.StartPosition = 'CenterScreen'

$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Point(75,170)
$okButton.Size = New-Object System.Drawing.Size(75,23)
$okButton.Text = 'OK'
$okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $okButton
$form.Controls.Add($okButton)

$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Point(150,170)
$cancelButton.Size = New-Object System.Drawing.Size(75,23)
$cancelButton.Text = 'Cancel'
$cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $cancelButton
$form.Controls.Add($cancelButton)

$label1 = New-Object System.Windows.Forms.Label
$label1.Location = New-Object System.Drawing.Point(10,20)
$label1.Size = New-Object System.Drawing.Size(280,20)
$label1.Text = 'Source-PC:'
$form.Controls.Add($label1)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,80)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Target-PC:'
$form.Controls.Add($label2)

$textBox1 = New-Object System.Windows.Forms.TextBox
$textBox1.Location = New-Object System.Drawing.Point(10,40)
$textBox1.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox1)

$textBox2 = New-Object System.Windows.Forms.TextBox
$textBox2.Location = New-Object System.Drawing.Point(10,100)
$textBox2.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox2)

$form.Topmost = $true

$form.Add_Shown({$textBox1.Select()})
$result = $form.ShowDialog()


if (($result -eq [System.Windows.Forms.DialogResult]::OK)) {
    $sourcePc = $textBox.Text
    $targetPc = $textBox2.Text
    
    $CopyFromPC = Get-ADComputer $sourcePc -Properties MemberOf
    $CopyToPC = Get-ADComputer $targetPc -Properties MemberOf

    if (($CopyFromPC.MemberOf | out-string) -eq ($CopyTopc.MemberOf | out-string)) {
        Write-Host "Groups are the same."
    } else {
        $CopyFromPC.MemberOf | Where {$CopyTopc.MemberOf -notcontains $_} | Out-GridView -PassThru | Add-ADGroupMember -Members $CopyTopc -verbose 
        [System.Windows.Forms.MessageBox]::Show("Groups got copied successfully","CompareGroups",0)
    }
}


  [1]: https://learn.microsoft.com/en-us/powershell/scripting/samples/creating-a-custom-input-box?view=powershell-7

Upvotes: 0

Views: 12507

Answers (1)

guiwhatsthat
guiwhatsthat

Reputation: 2434

You added a textbox called textbox1 and not textbox. Change the variablename from $sourcePc = $textBox.Text to $sourcePc = $textBox1.Text

Upvotes: 2

Related Questions