Switch statement

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 6 years and 2 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
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Switch statement

Post by apowershelluser »

Wasn’t sure if I should post this in the GUI or Script forum so I apologize.

I’m creating a form for techs to use and gather info from computers. One of my labels is for the Model of the PC. Due to the fact some SystemProductName (regkey value) are numbers (Lenovo) I’m forced to use switch statements to update the label to something more “easy on the eyes”. This application will be used by our front desk support and we all know the turn around in that environment. So it’s critical I don’t have a learning curve in the app.

I then had the idea of being able to use get-content from a .txt file or a import-csv and do that externally. For one specific reason.

It’s the only part of my form that is not “on the fly”.

Everything else, I just read a value that a cmd-let or function returns and update the corresponding elements of my form. IPaddress, MAC address, available disk space, etc...

So I’m trying to make an application that I’ll rarely have to modify and only update an external file for new and retiring PC models. Plus, with our PC upgrade cycle coming in the next year, I don’t want to redistribute this application to a 1000 techs due to updates in the switch statement.

This will also be used for the specific image on the PC ( due to our different areas, we create a regkey )

Make sense?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Switch statement

Post by jvierra »

What is your question or are you just discussing what you would like to do?
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Switch statement

Post by apowershelluser »

jvierra wrote: Fri Jan 19, 2018 4:26 am What is your question or are you just discussing what you would like to do?
My question is how do I move my switch statement from inside the script of my .exe to an external .txt or .csv file

This is what I currently have in my form that I want to move external
  1. $Model = switch ($model.trim())
  2.     {  
  3.         'HP EliteBook x360 1030 G2' { "HP EliteBook x360" }
  4.         'P147565' { "Lenovo P710" }
  5.         'Precision 7510' { "Dell 7510" }
  6.         'Latitude E7470' { "Dell 7470" }
  7. }
I'd like to not update my application, build it, redistribute it every time we have a model introduced into our environment
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Switch statement

Post by jvierra »

We would not use a switch statement. Use a ComboBox or ListBox loaded from a CSV file. A ComboBox will set itself according too the value it is bound to.
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Switch statement

Post by apowershelluser »

Well I'm updating a label, so it can't be either of those. Remember, I'm turning a reg key value, into an "easy on the eyes" label
switch.png
switch.png (8.29 KiB) Viewed 3223 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Switch statement

Post by jvierra »

You can easily use a combobox as a label.

You can also just filter the CSV:

$label.Text = ($csv | Where{$_.Column1 -eq 'somevalue'}).Column2
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Switch statement

Post by apowershelluser »

Hmmm.. I guess I'm confused. This is mostly my script to update the label.
  1. $computers = 'Computer1'
  2. $key = 'HARDWARE\DESCRIPTION\System\BIOS'
  3. $valuename = 'SystemProductName'
  4. $Model = foreach ($computer in $computers) {
  5.     $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
  6.     $regkey = $reg.opensubkey($key)
  7.     $regkey.getvalue($valuename)
  8. }
  9.  
  10. $Model = switch ($model.trim())
  11.  
  12.     {  
  13.  
  14.         'HP EliteBook x360 1030 G2' { "HP EliteBook x360" }
  15.  
  16.         'P147565' { "Lenovo P710" }
  17.  
  18.         'Precision 7510' { "Dell 7510" }
  19.  
  20.         'Latitude E7470' { "Dell 7470" }
  21.  
  22. }
  23.  
  24. $label1.text = $model
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Switch statement

Post by apowershelluser »

jvierra wrote: Fri Jan 19, 2018 6:27 am You can easily use a combobox as a label.

You can also just filter the CSV:

$label.Text = ($csv | Where{$_.Column1 -eq 'somevalue'}).Column2
Oh wait, ignore my last reply. Let me check out this $label.Text = ($csv | Where{$_.Column1 -eq 'somevalue'}).Column2
User avatar
apowershelluser
Posts: 194
Last visit: Mon Apr 15, 2024 3:21 pm
Answers: 2

Re: Switch statement

Post by apowershelluser »

jvierra wrote: Fri Jan 19, 2018 6:27 am You can easily use a combobox as a label.

You can also just filter the CSV:

$label.Text = ($csv | Where{$_.Column1 -eq 'somevalue'}).Column2
Yeeeeees!
  1. $computers = 'Server1
  2. $csv = Import-Csv '\\remotepath\Get-AppsModelSwitch.csv' -Delimiter ',' -Header @("Name", "Value")
  3. $key = 'HARDWARE\DESCRIPTION\System\BIOS'
  4. $valuename = 'SystemProductName'
  5. $Model = foreach ($computer in $computers) {
  6.     $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $computer)
  7.     $regkey = $reg.opensubkey($key)
  8.     $regkey.getvalue($valuename)
  9. }
  10.  
  11. $Model = ($csv | Where{$_.Name -eq $Model}).Value
User avatar
mxtrinidad
Posts: 399
Last visit: Tue May 16, 2023 6:52 am

Re: Switch statement

Post by mxtrinidad »

Any reason why not use the Get-WMIObject? It might get the information you want.

Get-WmiObject -Class Win32_ComputerSystem `
| Select-Object -Property Model, SystemFamily, SystemSKUNumber `
| Format-List;
## - Sample Output:
Model : 20HHCTO1WW
SystemFamily : ThinkPad P51
SystemSKUNumber : LENOVO_MT_20HH_BU_Think_FM_ThinkPad P51

You could use the existing "SystemSKUNumber" property to the create the 'Physical Model'.
Something like saving the result to a variable and then:

$SysPhyModel = Get-WmiObject -Class Win32_ComputerSystem `
| Select-Object -Property Model, SystemFamily, SystemSKUNumber;

($SysPhyModel.SystemSKUNumber).Split('_')[0]+" "+($SysPhyModel.SystemSKUNumber).Split('_')[6]

Sometimes is better to avoid working with registry keys. This is why we have the WMI cmdlets.

:)
This topic is 6 years and 2 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