1

I have a script that I am trying to turn into a GUI.

There is a DataGrid in place with 2 columns and I have two TextBoxes that I would like to be able to type something into that populates the DataGrid.

box1 populates the first column, box2 populates the second column.

I need help with the KeyDown and the population parts.

#LoadForm
./LoadDialog.ps1 -XamlPath 'C:\Forms\ReNamer.xaml'

#EVENT Handler
$NewName 

#Add old name manually
#################################PLEASE HELP HERE
$OldName.Add_KeyDown {
    if ($_.KeyCode -eq 'Enter') {
        $AddOldName.Invoke()
        #Suppress sound from unexpected use of enter on keyPress/keyUp
        $_.SuppressKeyPress = $true
    }
}
$AddOldName.Add_Click({ AddOldName })

##$renameBtn.Add_Click({ $Form.Close() })

$Import.Add_Click({ GetCompList })

#Launch the window
$xamGUI.ShowDialog() | Out-Null

#Csv import button
Function GetCompList {
    ##File location
    $csvfile = Import-Csv "C:\Sysinternals\rename.csv" |
        Select-Object @{ n = "OldName"; e = { $_.OldName } }, @{ n = "NewName"; e = { $_.NewName } }

    $csvfile | ForEach-Object { $dataGrid.AddChild($_) }
}

#Run Button
Function RenameComputers {
    Write-Host "Renaming computer from: $o to: $n"
    netdom renamecomputer $o /newName:$n /uD:sdirc\clarkj8 /passwordD:$p /force /reboot
}

#NewName txtbox

#OldName txtBox
##############################################PLEASE help here
Function AddOldName {
    $row = New-Object PSObject
    Add-Member -InputObject $row -MemberType NoteProperty -Name "OldName" -Value $OldName.Text
}
3
  • Hi, why roll my edit back? (I cleaned up and fixed your code) Commented Feb 2, 2017 at 17:43
  • I did not mean too. New to these forums and how it all works. i went in to edit out a username. and did it accidentally Commented Feb 2, 2017 at 17:45
  • OK, no problem. I've tried to fix things. Please take a look at the code and amend it if you wanted to change anything. Commented Feb 2, 2017 at 17:46

1 Answer 1

1
Function AddOldName {

    $row = New-Object PSObject

    Add-Member -InputObject $row -MemberType NoteProperty -Name "OldName" -Value $OldName.Text

    Add-Member -InputObject $row -MemberType NoteProperty -Name "NewName" -Value $NewName.Text

    $dataGrid.AddChild($row)

}

(assuming your new name field is called $newName)

And then in that function you could also do:

netdom renamecomputer $OldName.text /newName:$NewName.text /uD:sdirc\clarkj8 /passwordD:$p /force /reboot

And on your computer list import function you could also do:

$csvfile | % { netdom renamecomputer $_.OldName /newName:$_.NewName /uD:sdirc\clarkj8 /passwordD:$p /force /reboot }

I haven't tried KeyDowns in Powershell before though.

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

3 Comments

Thank you. That did get the function above working. Now i just need to get the KeyDown part correct.
This has worked on and off. Mostly it seems to just create a blank row. Function AddName{ $row = New-Object PSObject Add-Member -InputObject $row -MemberType NoteProperty -Name "Old Computer Name" -Value $OldName Add-Member -InputObject $row -MemberType NoteProperty -Name "New Computer Name" -Value $NewName $DataGrid.AddChild($row) }
Did you set the bindings to be "Old Computer Name" and "New Computer Name" in the xaml?

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.