PowerShell Studio Help

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
mqh777
Posts: 237
Last visit: Thu Jun 13, 2019 7:31 am

PowerShell Studio Help

Post by mqh777 »

I know how to script but I'm brand new to this tool. I created a blank form, I added 1 button to this form. When I press the button it runs this code: write-host "this is a test"

On my 1 button on the right hand side I have all of the properties. I do not see any property that stores the result of pressing the button. Example: If I press my button I want to capture "this is a test" in a variable so I can use it later.

how do you do that?

jvierra
Posts: 14759
Last visit: Fri Apr 16, 2021 10:47 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: PowerShell Studio Help

Post by jvierra »

Well the issue here is that you must know PowerShell before using forms. In PowerShell it is not possible to save the output of Write-Host to variable.

I recommend reviewing how variables are used in PowerShell as you will be making extensive use of variables in Forms.

You will also want to read all of the documentation on using PowerShell Studio with forms. Run the sample code an look closely at how it is done. There are also a number of blog articles on forms and controls that are very useful to learning PowerShell and using PowerShell with forms.

Have fun "forming".

User avatar
mqh777
Posts: 237
Last visit: Thu Jun 13, 2019 7:31 am

Re: PowerShell Studio Help

Post by mqh777 »

OK, fair enough. But what if it were not Write-Host? In this tool if you click on a button can you capture what that button click does into a variable?

Thanks.

jvierra
Posts: 14759
Last visit: Fri Apr 16, 2021 10:47 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: PowerShell Studio Help

Post by jvierra »

mqh777 wrote:OK, fair enough. But what if it were not Write-Host? In this tool if you click on a button can you capture what that button click does into a variable?

Thanks.
That is why I suggested the blog articles and the samples. They will show you all the ways to do this.

Actually there is no good one line answer for you questions. You need to learn what a form is and how to script a form. This is a complex and layered task. Once you actually try to build a form you will see that all things are events and in an event we can get and assign variables.

Start with the blog. I am sure it will answer most of your questions.

http://www.sapien.com/blog/topics/user- ... istrators/

When you have a script that you need help with then post back with a specific question.

jvierra
Posts: 14759
Last visit: Fri Apr 16, 2021 10:47 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: PowerShell Studio Help

Post by jvierra »

mqh777 wrote:OK, fair enough. But what if it were not Write-Host? In this tool if you click on a button can you capture what that button click does into a variable?

Thanks.
Let me clarify this a bit. A button click is just an event. During the button event you can do petty much anything. What you do depends on what meaning we have assigned to the button. If we decide that the button is called "Get Out Of Here" and we want it to close the form we would just execute "$form1.Close()" in the button event.

Maybe we want the button to save itself for future use. We could execute this line in the button event:

$script:lasbuttonclicked=$this

User avatar
mqh777
Posts: 237
Last visit: Thu Jun 13, 2019 7:31 am

Re: PowerShell Studio Help

Post by mqh777 »

thanks I did look at the sample code and I'm still struggling to find some basic stuff. How would I do the following.

Ask the user: Enter a computer name.

I don't see any Message buttons or ways to prompt the user. I see radio button, text, listbox, checkbox etc... but how do I prompt the user to enter a computer name? And then once I have the computer name I want to run a MSG command against it.

sorry for the noob questions :D

jvierra
Posts: 14759
Last visit: Fri Apr 16, 2021 10:47 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: PowerShell Studio Help

Post by jvierra »

You could do this:
temp.png
temp.png (9.08 KiB) Viewed 3310 times

jvierra
Posts: 14759
Last visit: Fri Apr 16, 2021 10:47 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: PowerShell Studio Help

Post by jvierra »

You could also do something like this:
PowerShell Code
Double-click the code block to select all.
function GetComputer{
    Add-Type -ass microsoft.visualbasic
    [microsoft.visualbasic.interaction]::Inputbox('Enter Computer Name','Get Computer',$env:computername)
    Write-Host "You entered $result" -fore green
]
GetComputer

User avatar
mqh777
Posts: 237
Last visit: Thu Jun 13, 2019 7:31 am

Re: PowerShell Studio Help

Post by mqh777 »

Thanks but I am not using VBScript :) Here is what we need and I need to know if this tool will do it.

I need to be able to run a MSG command (formerly NetSend) to either 1 machine or all machines. So my compiled Sapian.exe tool would run and have the following.



What is your MSG you want to send out? "I need an input field where they enter their text"

Send this to 1 computer? "enter computer name here"

Send this to all computers? "read AD for list of all computers"


It all has to be a GUI too....it can't bring up a cryptic PowerShell command prompt.

We have PowerShell Studio 2014 version 4.1.64 Can this tool what I just described?

Thank you and sorry if I was not clear enough to begin with.

jvierra
Posts: 14759
Last visit: Fri Apr 16, 2021 10:47 pm
Answers: 9
Has voted: 3 times
Been upvoted: 7 times

Re: PowerShell Studio Help

Post by jvierra »

The code posted was all pure PowerShell.

Locked