Issues Creating Simple Form (Newbie)

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.
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
bzowky
Posts: 7
Last visit: Mon Sep 16, 2019 3:13 pm

Issues Creating Simple Form (Newbie)

Post by bzowky »

Good Morning -

I'm new to PowerShell Studio and starting off by trying to convert a simple PS script to create test VMs in Hyper-V that I commonly edit and use into a single form GUI where I may enter values and click OK to execute. All the form consists of is 8 textboxes - each with label and a default value displayed. The value for each may be changed as desired then when ready, one may click the only button "Build" to execute the script using the variables defined by the textboxes. I'm using PowerShell Studio 2017 and have looked at numerous posts and documentation, but seem to be missing some things.

Below are my questions, screenshots of the design, current script, and original script I used for reference. Any help would be appreciated!

Questions
  • It seems that changes I make within the designer are not being reflected in the "Script" pane like when I enter default text for a textbox. Is there a button which populates the script with any changes made in the Designer, is it done during compile, or am I misunderstanding how it works?
  • When testing the script via console, I click "Build" but nothing happens - I just get another prompt. Basically, any assistance you can provide to help me get this working would be great. If easy to post a working solution that I could use for future reference, that would be ideal. :)

Form Design
form1.jpg
form1.jpg (33.78 KiB) Viewed 1301 times

Current Script

Code: Select all

$form1_Load={
	#TODO: Initialize Form Controls here
	
}

$labelVMName_Click={
	#TODO: Place custom script here
	
}

$textbox1_TextChanged={
	#TODO: Place custom script here
	
}

$labelCPUCores_Click = {
	#TODO: Place custom script here
	
}
# Assign Variables
$Name = $textbox1
$Cores = $textbox2
$RAM = $textbox3
$MaxRAM = $textbox4
$MinRAM = $textbox5
$VHDSize = $textbox6
$Boot = $textbox7
$VSwitch = $textbox8
$buttonBuild_Click = {
#Create VM
New-VM -Name $Name -MemoryStartupBytes $RAM -NewVHDPath "C:\Virtual Machines\$Name.vhdx" -NewVHDSizeBytes $VHDSize -Path "C:\Virtual Machines\$Name" -BootDevice CD -SwitchName $VSwitch -Generation 1
# Configure VM
Set-VMDvdDrive -VMName $Name -Path $Boot
Set-VMProcessor $Name -Count $Cores
Set-VM $Name -SnapshotFileLocation "C:\C:\Virtual Machines\Checkpoints\$Name" -AutomaticCheckpointsEnabled $false
Set-VMMemory $Name -DynamicMemoryEnabled $true -MinimumBytes $MinRAM -StartupBytes $RAM -MaximumBytes $MaxRAM
Add-VMNetworkAdapter -VMName $Name -IsLegacy $true -SwitchName $VSwitch
Remove-VMNetworkAdapter -VMName $Name -Name "Network Adapter"
}
Original Script (For Reference)

Code: Select all

# -----Create New VM Script -----
# -- Variables --
# VM Name
$Name = "OSDTest2"
# CPU
$CPU = 2
# Startup Memory
$RAM = 1GB
# Boot Media (Comment / uncomment lines below to select)
$Boot = "C:\ISOs\OSDMDTBootx64(1).iso"
# Virtual Switch
$VSwitch = "Internal"
#
#Create VM
New-VM -Name $Name -MemoryStartupBytes $RAM -NewVHDPath "C:\Virtual Machines\$Name\$Name.vhdx" -NewVHDSizeBytes 60GB -Path "C:\Virtual Machines\$Name" -BootDevice CD -SwitchName $VSwitch -Generation 1
# Configure VM
Set-VMDvdDrive -VMName $Name -Path $Boot
Set-VMProcessor $Name -Count 4
Set-VM $Name -SnapshotFileLocation "C:\C:\Virtual Machines\Checkpoints\$Name" -Auto
Set-VMMemory $Name -DynamicMemoryEnabled $true -MinimumBytes 1GB -StartupBytes 1GB -MaximumBytes 2GB
Add-VMNetworkAdapter -VMName $Name -IsLegacy $true -SwitchName $VSwitch
Remove-VMNetworkAdapter -VMName $Name -Name "Network Adapter"
Thanks, Guys!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues Creating Simple Form (Newbie)

Post by jvierra »

Since you are new to scripting with forms and with PSS I recommend taking some time to review the articles and videos on using PSS with forms.

To test a form we would use "Run" and not "Build".

Here are numerous articles describing how to do various things in PSS:

https://info.sapien.com/index.php/guis/gui-design-best-practice/user-interface-design-for-administrators

There are many useful videos here:

https://www.youtube.com/c/SAPIENTech

If you have a question I recommend asking a single simple question in a new topic. Be sure to give enough information to define the issue and any errors you are getting.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues Creating Simple Form (Newbie)

Post by jvierra »

Here is a quick fix for your code. It is not a complete fix but it will show you where some of your misunderstanding is and will help point you in a better direction. Overall I recommend taking time to learn about forms with PowerShell and to learn how to use PSS. The manual is linked on the "Help" ribbon.

Code: Select all

$form1_Load={
	#TODO: Initialize Form Controls here
	
}

$labelVMName_Click={
	#TODO: Place custom script here
	
}

$textbox1_TextChanged={
	#TODO: Place custom script here
	
}

$labelCPUCores_Click = {
	#TODO: Place custom script here
	
}

$buttonBuild_Click = {
    
    $Name = $textbox1.Text
    $Cores = $textbox2.Text
    $RAM = $textbox3.Text
    $MaxRAM = $textbox4.Text
    $MinRAM = $textbox5.Text
    $VHDSize = $textbox6.Text
    $Boot = $textbox7.Text
    $VSwitch = $textbox8.Text

    New-VM -Name $Name -MemoryStartupBytes $RAM -NewVHDPath "C:\Virtual Machines\$Name.vhdx" -NewVHDSizeBytes $VHDSize -Path "C:\Virtual Machines\$Name" -BootDevice CD -SwitchName $VSwitch -Generation 1
    # Configure VM
    Set-VMDvdDrive -VMName $Name -Path $Boot
    Set-VMProcessor $Name -Count $Cores
    Set-VM $Name -SnapshotFileLocation "C:\C:\Virtual Machines\Checkpoints\$Name" -AutomaticCheckpointsEnabled $false
    Set-VMMemory $Name -DynamicMemoryEnabled $true -MinimumBytes $MinRAM -StartupBytes $RAM -MaximumBytes $MaxRAM
    Add-VMNetworkAdapter -VMName $Name -IsLegacy $true -SwitchName $VSwitch
    Remove-VMNetworkAdapter -VMName $Name -Name "Network Adapter"
}
This topic is 5 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked