Few Questions About First True Project

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 7 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

Few Questions About First True Project

Post by bzowky »

Good Afternoon Guys!

Being an SCCM Administrator, I use PowerShell daily. I'm no expert at it, but know enough to piece things together and make things work. I purchased a license for PowerShell Studio a while back, but haven't used it until I thought it would be the perfect solution to build USB media for SCCM purposes. The only thing is that even after 3-4 hours working on it, I have a few questions, please. Below is an explanation of what I'm trying to do as well as all information about the project followed by the questions...

Explanation of Project If interested at all...
With our SCCM environment, we create two types of USB media - "Bootable" (~500mb) which is used to boot systems to be imaged over the network and "Standalone" which images systems using source data on the actual USB stick (~21gb.) The reason I want to put it in a GUI is that people are selecting the wrong distribution point to build it from causing ~20gb of data to start transferring over the WAN instead of local LAN. Yikes!

Plan for GUI
I have already created PowerShell scripts which when run will build the media automatically. My plan was to have 3 questions with radio buttons to choose answer followed by two buttons - one to create media and the other to exit the app. The plan was to have selection of radio buttons set certain variables then when they click "Create Media," it combines everything together.
OSDGUI.png
OSDGUI.png (26.13 KiB) Viewed 1246 times
Source Strings & Variables
Below are two execution strings which once variables are set from the radio buttons from the location and drive groups, will build the media. Samples of the variables are below them...

To Create OSD Boot Media on USB Stick
New-CMTaskSequenceMedia -BootableMediaOption -BootImage $boot -BootImageDistributionPoint $dp -BootImageManagementPoint $mp -MediaInputType USB -DriveName $drive -UserDeviceAffinity AutoApproval -MediaMode Dynamic -ProtectPassword 0 -MediaPath $drive

To Create OSD Standalone Media on USB Stick
New-CMTaskSequenceMedia -StandaloneMedia -MediaPath $drive -TaskSequence $ts -TaskSequenceDistributionPoint $dp -MediaInputType USB -DriveName $drive -ProtectPassword 0

Example of Variables
$dp = Get-CMDistributionPoint -SiteSystemServerName FQDNofdistributoinpoint#1
$ts = Get-CMTaskSequence -TaskSequencePackageID ABC001A4
$drive = "D:\"

Issues / Questions
1. How to configure radio button select to determine which string to execute?
If this project only offered to create on type of media, this would be simple as only one string is needed and all I'd have to do is create radio buttons to fill in variables. However, depending on what is selected in the first radio group, one of two different strings will be executed. That's why my included script is incomplete.What's the best way to do this?

2. Possible to (somewhat easily) be able to have options loaded as needed when started?
I'd much prefer having the script automatically select the location and thought the easiest way would be when run to have it ping 3 hostnames. Only one of the 3 (if any) would have a response time of under 10ms and if so it would be selected. If none have that time or below, a message will appear and you won't be able to create media. As for the USB media, I'm trying to find scripting which will examine drives, determine which have USB media attached, and if only one set it as the variable. Is this doable for someone at my level?

3. Importing the Module from File
These strings require that the SCCM module be imported and you be on the site's drive before they will work. Due to this, I added an "import-module filename.psd1" and "cd ABC:" lines under the "$OnLoadFormEvent" section. Is this correct? Surely there's a better way. I have it loading the .psd1 file from a share but didn't know if there was a way to import the profile into the project or similar.

Think that's about it - as if it wasn't enough. If you took the time to read this, I really appreciate it. I'll happily take whatever suggestions anyone may have. I had to swap out some hostnames since it wouldn't let me post with them. Thank you!!!

Current Script
Pretty sad this is all I have to show after 304 hours, but still learning :)
$OnLoadFormEvent={
#TODO: Initialize Form Controls here
import-module "\\server\share\ConfigurationManager.psd1"
cd ABC:
$mp = Get-CMManagementPoint -SiteCode ABC
$boot = Get-CMBootImage -Name "MDT x64 Boot Image"
}
$radiobuttonSite#1_CheckedChanged={
$dp = Get-CMDistributionPoint -SiteSystemServerName FQDNofdistributoinpoint#1

}
$radiobuttonSite#2_CheckedChanged={
#TODO: Place custom script here
$dp = Get-CMDistributionPoint -SiteSystemServerName FQDNofdistributoinpoint#2
}
$radiobuttonNewMexico_CheckedChanged={
#TODO: Place custom script here
$dp = Get-CMDistributionPoint -SiteSystemServerName FQDNofdistributoinpoint#3
}
$radiobuttonD_CheckedChanged={
#TODO: Place custom script here
$drive = "D:\"
}
$radiobuttonE_CheckedChanged={
#TODO: Place custom script here
$drive = "E:\"
}
$radiobuttonF_CheckedChanged={
#TODO: Place custom script here
$drive = "F:\"
}
$radiobuttonG_CheckedChanged={
#TODO: Place custom script here
$drive = "G:\"
}
$buttonCreateMedia_Click={
#TODO: Place custom script here
New-CMTaskSequenceMedia -BootableMediaOption -BootImage $boot -BootImageDistributionPoint $dp -BootImageManagementPoint $mp -MediaInputType USB -DriveName $drive -UserDeviceAffinity AutoApproval -MediaMode Dynamic -ProtectPassword 0 -MediaPath $drive
}
$buttonExit_Click={
#TODO: Place custom script here
$form1.Close()
return #use return if in an event block
}

Thanks again!!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Few Questions About First True Project

Post by jvierra »

[quote="bzowky
Issues / Questions
1. How to configure radio button select to determine which string to execute?
If this project only offered to create on type of media, this would be simple as only one string is needed and all I'd have to do is create radio buttons to fill in variables. However, depending on what is selected in the first radio group, one of two different strings will be executed. That's why my included script is incomplete.What's the best way to do this?

2. Possible to (somewhat easily) be able to have options loaded as needed when started?
I'd much prefer having the script automatically select the location and thought the easiest way would be when run to have it ping 3 hostnames. Only one of the 3 (if any) would have a response time of under 10ms and if so it would be selected. If none have that time or below, a message will appear and you won't be able to create media. As for the USB media, I'm trying to find scripting which will examine drives, determine which have USB media attached, and if only one set it as the variable. Is this doable for someone at my level?

3. Importing the Module from File
These strings require that the SCCM module be imported and you be on the site's drive before they will work. Due to this, I added an "import-module filename.psd1" and "cd ABC:" lines under the "$OnLoadFormEvent" section. Is this correct? Surely there's a better way. I have it loading the .psd1 file from a share but didn't know if there was a way to import the profile into the project or similar.

[/quote]

Quite a lengthy post. Let me begin by noting that complete project review is beyond the scope of all technical forums. I quickly read you discussion and extracted the questions which we can try to address.

1. Radio buttons have events like all other controls. The event is where you would execute code. Radio buttons are grouped within the parent container and can be extracted in a way that allows you to detect the selected button and its associated text.

2. YOU cab save values in many types of files and reload them at startup. Search for blog posts describing the various methods for initializing a script.

3. Modules stored in the $env:PsModulePath will auto load. Any module can be loaded by direct full path reference.

I recommend starting by reading and referring to the Sapien blog posts on GUI design and control usage.

https://www.sapien.com/blog/topics/user ... istrators/
https://www.sapien.com/blog/
https://www.sapien.com/blog/topics/spot ... -controls/

The following is the most comprehensive link to Sapien design resources: http://info.sapien.com/
This topic is 7 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