Issues Creating Simple Form (Newbie)
Posted: Wed Oct 24, 2018 9:50 am
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
Form Design
Current Script
Original Script (For Reference)
Thanks, Guys!
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
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"
}
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"