Powershell Studio: Have button read from textbox

I’m trying to get a button to read text from a textbox.

Example: I write “Computer name” in textbox. When I click the button, it executes code I assigned to it (such as finding the computer name’s Operating System).

I’m completely clueless where to start. The button currently executes on its own and does a Read-Host -prompt " Enter computer name" but I want to eliminate the prompt.

You will need to reference the text property of the text box. I would recommend looking at some of the samples provided with Powershell Studio.

In 2017: Click on Help on the Ribbon bar and click on Samples on the left hand side of the ribbon.

I would look at the ComputerPrompt example under the PowerShell studio folder as this addresses exactly what you want.

In this examples case to access the compute rname in the text file you would acess $txtComputerName.text

It’s a little helpful…

This is my code I had so far: Code

It prompts for a computer name and then upon enter it gives me a name, OS, and IP in a richtextbox. I’m afraid of having to completely change it because I want to read from text box.

The only thing you would need to change is the line:

$pc = Read-Host -Prompt " Enter Computer Name"

Put a text box element on the form and reference it. Assuming its called textbox1 you would do

$pc = $textbox1.text

Wow, that was too easy. Looks like I have some reading to do.

Two things, maybe you could help me:

  1. When I search by IP Address, it tells me it cannot find the description. Searching by hostname is no problem.

  2. What would be the easiest method with my script to perform a test-connection? That way if a wrong hostname or IP is entered, the script doesn’t freeze up.

I’ve added my updated code: Link. Right now I’m doing a wildcard search so that when I do type by IP, it still displays information with the description being blank.

So for your things there.

  1. Ad is not going to be able to find a computer via just the description - you are going to have to get the hostname of the computer in order to search for that.

  2. To test the connection you can use the test-connection command with a count of one and the quiet switch. The quiet switch returns either true or false only - which is nice to use in an if/then statement.

if(test-connection -computername $computer -count -quiet){
#do something

I have put all of this in a basic little script below - you can use this as a guide on how to handle your stuff - I used [system.net.dns] to convert get the host name of the ip address that is passed in through the text box. The same code is also able to be used if the hostname is passed in as well - so you have one set of code to handle both scenarios. Be aware that this is not fully tested code - it may fail - especially if there are instances where the computer is not registered on your dns server for whatever reason. Hopefully this is a good guide on getting you started with what you are looking to do - you obviously will need to add some logic in there if no computers are found to have connections/etc


Somehow I screwed up adding the GIST


I tried your script verbatim, to see what I’d need to change if anything.

The good news is that while it doesn’t “error” or give you any indication of failure, if you type an IP/Hostname that isn’t reachable, it doesn’t freeze up anymore; after a few seconds cursor will go back to the TextBox.

Is there a way in $presearchComputer to do a wildcard? Say for example just using the first two octets (192.168..)?

If you are having these questions, i highly recommend the learn powershell toolmaking in a month of lunches book.

if you just follow the exercises in the book, you will have all of these basic questions answered